【问题标题】:XmlElement.Attributes is dropping namespacesXmlElement.Attributes 正在删除命名空间
【发布时间】:2023-03-07 00:11:01
【问题描述】:

如果您更喜欢小提琴 - click here

在我的调试器中,我有一个 XmlElement.OuterXml:

<?xml version="1.0"?>
<p:sld mc:Ignorable="p14 a14 p15 p16 a16 thm15 adec ahyp v"
  xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:asvg="http://schemas.microsoft.com/office/drawing/2016/SVG/main"
  xmlns:ahyp="http://schemas.microsoft.com/office/drawing/2018/hyperlinkcolor"
  xmlns:adec="http://schemas.microsoft.com/office/drawing/2017/decorative"
  xmlns:thm15="http://schemas.microsoft.com/office/thememl/2012/main"
  xmlns:p16="http://schemas.microsoft.com/office/powerpoint/2015/main"
  xmlns:p15="http://schemas.microsoft.com/office/powerpoint/2012/main"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" 

注意它是 mc:Ignorable,而不是 Ignorable。

我调用 XmlElement.Attributes 并且其中一个属性用于 Ignorable 但是...

它丢失了前缀/命名空间。

知道这里发生了什么吗?

更新:I also posted on MSDN 到目前为止的答案是这是一个错误,我很惊讶这么基本的东西会出错。

【问题讨论】:

  • 前缀无关紧要。它绝对可以是任何东西。最主要的是该属性有一个命名空间。
  • 它确实在那里。如果您为整个 XmlDocument doc 生成 XML,那么您将获得 &lt;inner mc:Ignorable="p14 a14 p15 p16 a16 thm15 adec ahyp v" /&gt;,请参阅 dotnetfiddle.net/v9v36E。我不确定为什么 XmlAttribute.Name 中不存在前缀,因为据记录它会返回 属性节点的限定名称。
  • 老实说,您应该考虑切换到LINQ to XML,它更容易使用。值得注意的是,它对命名空间的处理比XmlDocument 简单一些。

标签: c# xml system.xml


【解决方案1】:

您可能无法直接将其作为属性名称的一部分。有一个属性 "Prefix" ,预计会返回,但它是空的。但是GetPrefixOfNamespace 在将属性的命名空间 URI 作为输入传递后会正确返回前缀。可能是一些实施问题。所以现在尝试如下所述。

var list = inner.Attributes;
foreach (System.Xml.XmlAttribute attr in list)
    Console.WriteLine(attr.GetPrefixOfNamespace(attr.NamespaceURI) + ":" + attr.Name);

【讨论】:

    猜你喜欢
    • 2010-12-14
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2020-04-06
    • 2014-12-11
    相关资源
    最近更新 更多