Mike
Mike

Reputation: 6050

Reading an xml file with XDoc

I am trying to read the following file (partially presented)

<?xml version="1.0" encoding="utf-8"?>
 <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.v1="urn:schemas-microsoft-com:clickonce.v1"
 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:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity name="program.application" version="4.0.0.27077" publicKeyToken="8f99fe9fddfae125e" language="neutral" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />

I have

Dim manifestFile As String = webClient.DownloadString(location)
Dim xdoc As New Xml.XmlDocument()
xdoc.LoadXml(manifestFile)

I have tried

xdoc.SelectSingleNode("//asmv1:assemblyIdentity")
xdoc.SelectSingleNode("//assemblyIdentity")

Dim nsSys As Xml.XmlNamespaceManager = New Xml.XmlNamespaceManager(xdoc.NameTable)
nsSys.AddNamespace("def", "urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd")
xdoc.SelectSingleNode("//asmv1:assemblyIdentity",ns)
xdoc.SelectSingleNode("//assemblyIdentity",ns)
xdoc.SelectSingleNode("//def:assemblyIdentity",ns)

All return Nothing

What is the correct path to select the <assemblyIdentity> node?

Note I cannot use LinqToXml

Thank you

Upvotes: 2

Views: 491

Answers (1)

maxedev
maxedev

Reputation: 941

Assuming I close the tag - file looks like this:

<?xml version="1.0" encoding="utf-8"?>
    <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.v1="urn:schemas-microsoft-com:clickonce.v1" 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:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <assemblyIdentity name="program.application" version="4.0.0.27077" publicKeyToken="8f99fe9fddfae125e" language="neutral" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
    </asmv1:assembly>

The following works for me:

Dim doc As New XmlDocument()
Dim nsmgr As New XmlNamespaceManager(doc.NameTable)

doc.Load("c:\temp\temp2.xml")
nsmgr.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1")

Dim node As XmlNode = doc.SelectSingleNode("//asmv1:assemblyIdentity", nsmgr)
MessageBox.Show(node.OuterXml)

and returns:

<assemblyIdentity name="program.application" version="4.0.0.27077" publicKeyToken="8f99fe9fddfae125e" language="neutral" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />

The <assemblyIdentity> node references the asmv1 namespace, so that's the one that needs to be added to the XMLNamespaceManager

Upvotes: 3

Related Questions