Click here to Skip to main content
15,790,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have xml files that look like
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" xmlns:dsig="" xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1">
  <asmv1:assemblyIdentity name="ClickOnceAppTest.exe" version="" publicKeyToken="d1e81ecbb61bc9b1" language="neutral" processorArchitecture="x86" type="win32" />
  <application />
    <assemblyIdentity name="ClickOnceAppTest" version="" language="neutral" processorArchitecture="x86" />
    <commandLine file="ClickOnceAppTest.exe" parameters="" />

and so on.

I need to parse it, by extracting certain tags that match by criteria (tagname, attributes).

So far I found 2 methods to search by criteria:
1.LINQ on XDocument
2.XmlDocument's SelectNodes method using XPath.

In either case I can't do that because I don't quite understand namespacing. When I try to read xml with XDocument, it prepends some asmv2: stuff to inner xml nodes. For example, in source I have
    <dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="ClickOnceAppTest.exe" size="26112">
      <assemblyIdentity name="ClickOnceAppTest" version="" language="neutral" processorArchitecture="x86" />

but XDocument prepends asmv1 yielding <asmv1:dependency xmlns:asmv1="#unknown"> and so xDoc.Descendants("dependency") yields nothing.
With XPath I get saame no results situation. I've tried to write
XmlNamespaceManager mngr = new XmlNamespaceManager(serverXml.NameTable);
string xPath="assembly/dependency/dependentAssembly/";
XmlNodeList emptySet = serverXml.SelectNodes(xPath, mngr);

So, how do I tackle the problem with namespaces?

P.S. Sorry for such a cumbersome style - my moral is down by learning that I am no good at some elementary stuff.
Updated 29-Oct-10 6:44am

1 solution

I think it would get easier and more efficient when using a XmlReader instead of XmlDocument. For handling the namespaces you would need to do more with the XmlNamespaceManager. You could remove the namespaces for examples. Have a look how to control those namespaces:[^]

You can also retrieve the namespace for each element:[^]

Good luck!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900