我如何删除使用Mage.exe/MageUI.exe创建的Manifest-File的节点而不会遇到命名空间的问题

本文关键字:exe 节点 Manifest-File 创建 问题 命名空间 遇到 MageUI 何删除 删除 Mage | 更新日期: 2023-09-27 18:16:49

在过去的几天里,我试图在清单文件中.Remove();一些节点,以指定问题,我试图用c#程序删除清单文件中的填充文件。

如果你不知道什么是填充文件,我将尽可能地解释它:

一个被填充的文件是一个文件(例如:" populed -File.txt.deploy"),其中一个你需要你的Manifest-File,因为没有populed - files你的应用程序不会做那么多。当您按下MageUI.exe中的"填充"按钮或在MageUI.exe中设置选项"-FromDirectory"时,您将在Manifest-File中添加节点,它们称为<dependency dependencyType="install"></dependency><file></file>。在这些节点中是关于已填充文件的信息(例如:name=" populatedfile .txt.deploy" size="123" location="…")。等等)。

这些节点就像这样在Manifest-File中:

应该注意第二个节点:<asmv1:assembly>,因为如果使用MageUi.exe/Mage.exe创建Manifest-File是默认的,那么问题就开始了。

<?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);

到目前为止一切顺利,但是当我为<file></file> -节点添加.Remove();时,代码看起来像这样:

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);

我注意到Manifest-File已经添加,在几乎每个节点的前面,asmv2:<dependency> -节点被删除了。当<dependency>/<file> -节点被删除时,将会有任何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"

最后:我的问题是如何解决这个问题?

一如既往:你可以编辑这个,使它更具体,甚至更正确(也许我没有解释填充文件的权利)请以任何方式纠正我,但要有建设性:)

我如何删除使用Mage.exe/MageUI.exe创建的Manifest-File的节点而不会遇到命名空间的问题

好吧,看起来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>