首页 > 解决方案 > XmlElement.Attributes 正在删除命名空间

问题描述

如果您喜欢小提琴,请单击此处

在我的调试器中,我有一个 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 但是...... 在此处输入图像描述

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

知道这里发生了什么吗?

更新:我还在 MSDN 上发布了到目前为止的答案是这是一个错误,我很惊讶这么基本的东西会出错。

标签: c#xmlsystem.xml

解决方案


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

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

推荐阅读