在最近的几天里,我尝试.Remove();
了清单文件中的某些节点,以指定问题,我尝试使用C#程序删除清单文件中的填充文件。
如果您不确切知道什么是填充文件,我将尽我所能解释:
填充文件是一个文件(例如:“ Populated-File.txt.deploy”),其中一个文件就是清单文件,因为没有填充文件,您的应用程序将无法执行那么多操作。当您按下MageUI.exe中的“填充”按钮或在Mage.exe中设置选项“ -FromDirectory”时,您将在清单文件中添加节点,它们被称为<dependency dependencyType="install"></dependency>
和<file></file>
。在这些节点中是有关填充文件的信息(例如:name =“ Populated-File.txt.deploy” size =“ 123” location =“ ...”,依此类推)。
这些节点位于清单文件中,如下所示:
应注意第二个节点:<asmv1:assembly>
,因为如果使用MageUi.exe / Mage.exe创建了清单文件,则默认情况下会出现问题。
<?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" />
<description asmv2:iconFile="Example.ico" xmlns="urn:schemas-microsoft-com:asm.v1" />
<dependency>
<dependentAssembly dependencyType="install" size="123">
</dependentAssembly>
</dependency>
<file name="PopulatedFile.dll.deploy" size="123">
</file>
</asmv1:assembly>
一段时间后,我得到了这个论坛的帮助。
//does currently only delete the "<dependency></dependency>"-Node
xml.Descendants().Where(x => x.Name.LocalName == "dependentAssembly" && (string)x.Attribute("dependencyType") == "install").Select(x => x.Parent).Remove();
xml.Save(filePath);
到目前为止一切顺利,但是正如我.Remove();
为<file></file>
-Node添加的那样,代码看起来像这样:
xml.Descendants().Where(x => x.Name.LocalName == "dependentAssembly" && (string)x.Attribute("dependencyType") == "install").Select(x => x.Parent).Remove();
xml.Descendants().Where(x => x.Name == "file").Remove();
xml.Save(filePath);
我注意到清单文件已添加,几乎在每个Nodeasmv2:
的<dependency>
前面,并且-Nodes被删除。这将是最好的时候<dependency>
/ <file>
-nodes遭到删除或会有什么asmv2
。
就像这样:
<?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" />
<description asmv2:iconFile="Example.ico" xmlns="urn:schemas-microsoft-com:asm.v1" />
<asmv2:file name="PopulatedFile.dll.deploy" size="123">
</asmv2:file>
</asmv1:assembly>
这样做的结果是该文件无法再使用MageUi.exe打开。
因此,我也开始在该论坛上进行研究,并得到其命名空间作为答案。 xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
所以最后:我的问题是我该如何解决?
一如既往:您可以对其进行编辑,以使其变得更加具体甚至更正确(也许我没有解释“ Populate-Files”(正确的“ Populate-Files”))。
好的,看起来LINQ to XML感到困惑,因为名称空间在根元素中被声明了两次-一次是默认名称空间,一次是别名asmv2
。最简单的解决方案是删除该属性。
老实说,MageUI应该应对它-它代表了正确的数据。显然,还有另一个程序没有正确看待XML。(叹...)
我还将寻找具有正确名称(在正确的名称空间中)的后代,而不是查找特定的LocalName
。例如:
var doc = XDocument.Load("test.xml");
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("test2.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="Example.exe" />
<description p8:iconFile="Example.ico"
xmlns="urn:schemas-microsoft-com:asm.v1"
xmlns:p8="urn:schemas-microsoft-com:asm.v2" />
</asmv1:assembly>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句