gpt4 book ai didi

c# - 如何删除属性的命名空间前缀?

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:31 25 4
gpt4 key购买 nike

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

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 的新命名空间并且此 Namespce 不允许我更新 Xmlfile(使用 Mage.exe)所以解决方案是:<description iconFile="Icon.ico" xmlns="urn:schemas-microsoft-com:asm.v1" />

或:<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();
}

关于c# - 如何删除属性的命名空间前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25197893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com