MSXML2 Works, but SYSTEM.XML Does not!

vinnie881

Well-known member
Joined
Sep 3, 2006
Messages
152
Programming Experience
3-5
I'm racking my head over this!!!

I have a simple XML file
VB.NET:
Expand Collapse Copy
<?xml version="1.0" encoding="utf-8"?>
<GetPhoneInfoResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.serviceobjects.com/">
  <PhoneInfo>
    <PhoneNumberIn>18556955554</PhoneNumberIn>
    <PhoneNumberClean>8556955554</PhoneNumberClean>
    <Providers>
      <Provider>
        <Name>AMERITECH ILLINOIS</Name>
        <City>NEWARK</City>
        <State>ILLINOIS</State>
        <LineType>LANDLINE</LineType>
        <Latitude>41.5177</Latitude>
        <Longitude>-88.5187</Longitude>
      </Provider>
    </Providers>
    <Contacts>
      <Contact>
        <Name>JOHN DOE</Name>
        <Address>1234 Nothing</Address>
        <City>NEWARK</City>
        <State>IL</State>
        <Zip>12345</Zip>
        <Type>RESIDENTIAL</Type>
        <Quality>HIGH</Quality>
      </Contact>
    </Contacts>
  </PhoneInfo>
  <TokensUsed>65</TokensUsed>
  <DEBUG />
</GetPhoneInfoResult>

It loads and works perfect when I use msxml2, but I can't use msxml2 because of another issue.

Anyways, in the code below
VB.NET:
Expand Collapse Copy
'THIS WORKS
     Dim y As New MSXML2.DOMDocument30
            y.loadXML(result)
            Dim d As MSXML2.IXMLDOMNode = y.selectSingleNode("//Contacts/Contact")

'THIS DOES NOT!!!
dim _str as System.xml.XMLDOCUMENT
            _Str.LoadXml(result)
            Dim n As System.Xml.XmlNode = _Str.SelectSingleNode("//Contacts/Contact")

Anyone have anyclue why this could be? When I look at the node counts under the documents in the msxml2 doc there are 9, but in the xmldoc there are 3?

THis makes no sense. Please help me in figureing out how to select nodes and get this to work using the XMLDocument, or any other alternative that doesn't require needing to add a referance to the project.

Thanks
 
To test please copy the XML to a file, and you can then load

VB.NET:
Expand Collapse Copy
dim _str as new system.xml.Xmldocument
            _Str.Load("C:\Myxml.xml")
            Dim n1 As System.Xml.XmlNode = _Str.SelectSingleNode("//Contacts/Contact")

There is nothing I can do to select a node I want?? It works flawless with msxml, so this just is puzzling!!

Thanks!
 
You're forgetting namespaces:
VB.NET:
Expand Collapse Copy
Dim ns As New Xml.XmlNamespaceManager(doc.NameTable)
ns.AddNamespace("pre", "http://www.serviceobjects.com/")
Dim firstContact As System.Xml.XmlNode = doc.SelectSingleNode("//pre:Contacts/pre:Contact", ns)
 
I tried adding that, and it and it still doesn't seem to be working. Can you please provide an example using the doc I provided?
 
That was what I just did, the code selects the first Contacts/Contact node.
 
Your solution works
VB.NET:
Expand Collapse Copy
dim doc as new system.xml.Xmldocument
            doc.Load("C:\Myxml.xml")
Dim ns As New Xml.XmlNamespaceManager(doc.NameTable)
ns.AddNamespace("pre", "http://www.serviceobjects.com/")
Dim firstContact As System.Xml.XmlNode = doc.SelectSingleNode("//pre:Contacts/pre:Contact", ns)

Is there anyway I can set the default namespace so I can do a

doc.SelectSingleNode("//Contacts/Contact")

and it will return properly?
 
Last edited:
I confirm again, we are using the exact same code and data, and the Contact node is selected. All nodes of your data belongs to the qualified namespace as used in code.
 
Please see my edited response. It did work, it was a coding error on my part I'm sorry for the confusion.

Is there anyway I can set the default namespace so I can do a

doc.SelectSingleNode("//Contacts/Contact")

and it will return properly?
 
Is there anyway I can set the default namespace so I can do a
No, Xpath expressions can't use the default namespace of the XmlNamespaceManager. It would be convenient if they could though.
 
This is not a good practice, but I was able to get it to work by replaceing the namespace prior to loading.

VB.NET:
Expand Collapse Copy
dim result as string = doc.xml
            result = Replace(result, " xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://www.serviceobjects.com/""", "")
dim tt as new system.xml.XMLdocument
            tt.LoadXml(result)
                        Dim n As System.Xml.XmlNode = tt.SelectSingleNode("//Contacts/Contact")

Obviously it's not good to edit the xml doc, but since the method I illustrated does work, is there anyway to remove the namespaces in a better mannor?

Thanks!
 
In this simple case I could think of this:
VB.NET:
Expand Collapse Copy
doc.DocumentElement.SetAttribute("xmlns", "")
Then save and reload the document.
 
Back
Top