Click here to Skip to main content
15,441,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
I have xml files that look like
XML
<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="http://www.w3.org/2000/09/xmldsig#" 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="http://www.w3.org/2001/XMLSchema-instance" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1">
  <asmv1:assemblyIdentity name="ClickOnceAppTest.exe" version="1.0.0.4" publicKeyToken="d1e81ecbb61bc9b1" language="neutral" processorArchitecture="x86" type="win32" />
  <application />
  <entryPoint>
    <assemblyIdentity name="ClickOnceAppTest" version="1.0.0.1" language="neutral" processorArchitecture="x86" />
    <commandLine file="ClickOnceAppTest.exe" parameters="" />
  </entryPoint>
  <trustInfo>
    <security>
      <applicationRequestMinimum>

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
XML
<dependency>
    <dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="ClickOnceAppTest.exe" size="26112">
      <assemblyIdentity name="ClickOnceAppTest" version="1.0.0.1" 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
C#
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.
Posted
Updated 29-Oct-10 5:44am
v3

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:
http://msdn.microsoft.com/en-us/library/d6730bwt.aspx[^]

You can also retrieve the namespace for each element:
http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.namespaceuri[^]

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