In the Last few Days i tried to .Remove();
some Nodes in a Manifest-File , to specify the Problem i tried to delete the Populated-Files in a Manifest-File with a C# programm.
If you don't know exactly what a Populated file is i will explain it as good as i can:
A Populated File is a File (example: "Populated-File.txt.deploy") wich one you need for you're Manifest-File, because without Populated-Files your Application won't do that much. When you press the "Populate"-Button in the MageUI.exe or you set the Option "-FromDirectory" in the Mage.exe you will add Nodes in the Manifest-File, they are called <dependency dependencyType="install"></dependency>
and <file></file>
. In these Nodes are Informations about the Populated-Files(example: name="Populated-File.txt.deploy" size="123" location="..."and so on).
These Nodes are in the Manifest-File just like that:
Attention should be paid to the 2nd Node : <asmv1:assembly>
, because that is default if the Manifest-File got created with MageUi.exe/ Mage.exe, there starts the Problem.
<?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>
After a while I got it, with some help from this Forum.
//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);
So far so good, but as i added .Remove();
for the <file></file>
-Node that the Code looked like that:
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);
I noticed that the Manifest-File has added, infront of almost every Node, asmv2:
and the <dependency>
-Nodes got deleted. It would be better when <dependency>
/<file>
-Nodes get deleted and there would be any asmv2
.
Just like that:
<?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>
The consiquence is that the File is not openable anymore with MageUi.exe.
So i started to research also on this Forum, and got as answer its the Namespace.
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
So finally: My question is How could i solve this?
As always: You can edit this so it gets more specific or even more correnct (maybe i didn't explained the Populate-Files right) Please correct me in any way but be constructive :)
Okay, it looks like LINQ to XML is getting confused because the namespace is declared twice in the root element - once as the default namespace, and once with an alias of asmv2
. The simplest solution is just to remove that attribute.
To be honest, MageUI should cope with it anyway - it's representing the right data. Yet another program that doesn't look at the XML properly, apparently. (Sigh...)
I'd also look for descendants with the right name (in the right namespace) rather than looking for a particular LocalName
. For example:
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");
Output (reformatted a little):
<?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>
See more on this question at Stackoverflow