.Remove ();不移除和.save ();在每个Node前面设置asmv
本文关键字:Node 前面 asmv 设置 Remove save | 更新日期: 2023-09-27 18:16:21
正如我在Title中所说:
.Remove();
不移除
和
.Save();
在每个节点前设置asmv2
private void DeleteXmlPopulates()
{
string filePath = "C:''Example''Example.exe.manifest"
var xml = XElement.Load(filePath);
xml.Descendants().Where(x => x.Name == "dependentAssembly" && (string)x.Attribute("dependencyType") == "install").Remove();
xml.Save(filePath);
}
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: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="Example.exe" version="6.1.0.0" publicKeyToken="7f785aa0b92a51a3" language="neutral" processorArchitecture="x86" type="win32" />
<description asmv2:iconFile="Example.ico" xmlns="urn:schemas-microsoft-com:asm.v1" />
<application />
<entryPoint>
<assemblyIdentity name="Example" version="6.1.0.14132" language="neutral" processorArchitecture="x86" />
<commandLine file="Example.exe" parameters="" />
</entryPoint>
<trustInfo>
<security>
<applicationRequestMinimum>
<PermissionSet Unrestricted="true" ID="Custom" SameSite="site" />
<defaultAssemblyRequest permissionSetReference="Custom" />
</applicationRequestMinimum>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentOS>
<osVersionInfo>
<os majorVersion="5" minorVersion="1" buildNumber="2600" servicePackMajor="0" />
</osVersionInfo>
</dependentOS>
</dependency>
<dependency>
<dependentAssembly dependencyType="preRequisite">
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install">
</dependentAssembly>
</dependency>
Xml文件中有注释:UAC Manifest Options If you want to change the Windows User Account Control level replace the requestedExecutionLevel node with one of the following. <requestedExecutionLevel level="asInvoker" uiAccess="false" /> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> If you want to utilize File and Registry Virtualization for backward compatibility then delete the requestedExecutionLevel node.
代码应该是什么样的:(只是依赖部分,其余部分应该相同)
<dependency>
<dependentAssembly dependencyType="preRequisite" allowDelayedBinding="true">
</dependentAssembly>
</dependency>
代码看起来是这样的:(只是依赖部分,其余部分在每个节点前面都有asmv2)
<asmv2:dependency>
<asmv2:dependentAssembly dependencyType="preRequisite">
</asmv2:dependentAssembly>
</asmv2:dependency>
<asmv2:dependency>
<asmv2:dependentAssembly dependencyType="install" >
</asmv2:dependency>
</asmv2:dependentAssembly>
好的,更重要的问题(分别是我们将尝试回答的问题)是为什么.Save();
将asmv2:
设置在每个节点的前面?如果这是正常的.Save();
方法,那么我很惊讶,因为文件不能再打开MageUI.exe
我该如何解决或避免这个问题?
我将感激建设性的批评:)
似乎是一个经典的-
混合声明式代码/命令式代码bug (LINQ to XML)你正在删除项目,同时对它们执行查询,但Save
工作,因为它没有任何这样的问题。
xml.Descendants().Where(x => x.Name == "dependentAssembly" && (string)x.Attribute("dependencyType") == "install").Remove();
您应该尝试使用单独的列表来删除它们。正如MSDN中提到的-
http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.remove (v = vs.110) . aspx
在LINQ to XML编程中,不应该操作或修改集合在查询该集合中的节点时。在实际换句话说,这意味着您不应该遍历一组节点和移除它们。相反,您应该将它们具体化到List by中使用ToList扩展方法。然后,你可以迭代删除节点的列表。有关更多信息,请参见混合声明性代码/命令式代码bug (LINQ to XML)。
或者,如果如果需要删除一组节点,建议使用扩展。删除的方法。此方法将节点复制到列表中,并且然后遍历列表以删除节点。
理想的解决方案应该类似于-
var itemsToRemove = xml.Descendants().Where(x => x.Name == "dependentAssembly" && (string)x.Attribute("dependencyType") == "install").ToList();
itemsToRemove.Remove();
示例- http://msdn.microsoft.com/en-us/library/bb357554(v=vs.110).aspx
您的XML具有默认名称空间(xmlns="urn:schemas-microsoft-com:asm.v2"
)。您需要使用" XNamespace
+元素名称"来引用名称空间:
XNamespace ns = "urn:schemas-microsoft-com:asm.v2";
xml.Descendants()
.Where(x => x.Name == ns+"dependentAssembly" && (string)x.Attribute("dependencyType") == "install")
.Remove();
或者使用XElement.Name.LocalName
xml.Descendants()
.Where(x => x.Name.LocalName == "dependentAssembly" && (string)x.Attribute("dependencyType") == "install")
.Remove();