如何移除属性的名称空间前缀

本文关键字:空间 前缀 何移 属性 | 更新日期: 2023-09-27 18:17:08

在过去的几天里,我试图删除一个节点的命名空间的前缀。在一些帮助下,我完成了它:

Xml(前):

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1assembly.adaptive.xsd"
manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
xmlns="urn:schemas-microsoft-com:asm.v2"
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1"
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"
xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"
xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
 <asmv1:assemblyIdentity name="Executable.exe"/>
 <description asmv2:iconFile="Icon.ico" xmlns="urn:schemas-microsoft-com:asm.v1" />
  <dependency>
    <dependentAssembly dependencyType="preRequisite">
    </dependentAssembly>
  </dependency>
  <dependency>
    <dependentAssembly dependencyType="install">
    </dependentAssembly>
  </dependency>
    <file name="Populatedfile.dll.deploy" size="123">
    </file>

c#代码:

 var doc = XDocument.Load("Xmlfile");
 doc.Root.Attribute(XNamespace.Xmlns + "asmv2").Remove();
 XNamespace ns = "urn:schemas-microsoft-com:asm.v2";
 doc.Descendants(ns + "dependentAssembly")
     .Where(x => (string)x.Attribute("dependencyType") == "install")
     .Select(x => x.Parent)
     .Remove();
 doc.Descendants(ns + "file").Remove();
 doc.Save("XmlFile");
Xml(后):

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd"
manifestVersion="1.0"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns="urn:schemas-microsoft-com:asm.v2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1" 
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"
xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" 
xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
<asmv1:assemblyIdentity name="Executable.exe" />
<description p8:iconFile="Icon.ico" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:p8="urn:schemas-microsoft-com:asm.v2" />
<dependency>
 <dependentAssembly dependencyType="preRequisite">
 </dependentAssembly>
</dependency>

所以问题是:

正如我们在第二个Xml文件(Xml后)中看到的,将iconFile的前缀从asmv2:更改为p8,并在同一节点(<description>)中添加了一个名为p8的新命名空间,这个命名空间不允许我更新Xmlfile (with Mage.exe)所以解决方案是:<description iconFile="Icon.ico" xmlns="urn:schemas-microsoft-com:asm.v1" />

or: <description asmv2:iconFile="Icon.ico" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"/>

问题是:

如何进入<description>节点删除/重命名(如果可能的话)p8命名空间?

我尝试了:doc.Root.Attribute(XNamespace.Xmlns + "p8").Remove();,但它抛出了一个名为:System的异常。得到NullReferenceException

请帮我一下:)

一如既往,只要你是建设性的,你可以纠正我:)

更新/答:

    var doc = XDocument.Load("Xmlfile");
    try
    {
        doc.Root.Attribute(XNamespace.Xmlns + "asmv2").Remove();
    }
    catch (NullReferenceException) {}
    XNamespace ns = "urn:schemas-microsoft-com:asm.v2";
    doc.Descendants(ns + "dependentAssembly")
       .Where(x => (string)x.Attribute("dependencyType") == "install")
       .Select(x => x.Parent)
       .Remove();
    doc.Descendants(ns + "file").Remove();
    doc.Save(filePath);
    foreach (var attr in doc.Descendants()
                            .SelectMany(d => d.Attributes())
                            .Where(a => a.Name.Namespace == ns))
     {
        attr.Parent.Add(new XAttribute(attr.Name.LocalName, attr.Value));
        attr.Remove();
     }
     doc.Save(filePath);
    }

如何移除属性的名称空间前缀

如果我正确理解了您的需求,您需要以下内容:

foreach (var attr in doc.Descendants()
                        .SelectMany(d => d.Attributes())
                        .Where(a => a.Name.Namespace == ns))
{
   attr.Parent.Add(new XAttribute(attr.Name.LocalName, attr.Value));
   attr.Remove();
}