I have been learning VB.NET and am currently having troubles retrieving data from an XML file. I'm trying to keep the code as simple as possible as I only need to select single items from an XML file, not rows of data.

The XML file contains:

<CurrentPeakUsage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://au.com.amnet.memberutils/">
    <Summary xmlns="http://amcom.com.au/UsageWebServices">
    <RateLimits xmlns="http://amcom.com.au/UsageWebServices" />
    <RateGroupName xmlns="http://amcom.com.au/UsageWebServices">ADSL 2+ Enabled 45G/65G - $49.00</RateGroupName>
    <Allowances xmlns="http://amcom.com.au/UsageWebServices">
        <Name>Off-Peak Basic</Name>
        <OctetsOut xsi:nil="true" />
        <Name>Off-Peak Peering</Name>
        <OctetsOut xsi:nil="true" />
        <Name>Peak Basic</Name>
        <OctetsOut xsi:nil="true" />
        <Name>Peak Peering</Name>
        <OctetsOut xsi:nil="true" />

My code contains:

Dim xmlData As XDocument = XDocument.Load("C:\data.xml")
        Dim xmlContents As String = xmlData...<Customer>.Value
        TextBox1.Text = xmlContents

While the msgbox will contain the entire XML document, so it is loading correctly, I can't pull the single element value. Am I understanding Xdocuments and XML data handling in VB.NET completely wrong?

XPath is firmly established as the standard for navigation or location of data in XML documents. Microsoft has taken XML into the platform. NET, and soon to be released, Microsoft Office 2003 includes XPath support.

I've been following a few examples online trying to get my end result. I started with XPath using the code below (same XML data) but kept getting NULL returns.

Dim MyXpath As String
        Dim MyXMLDocument As System.Xml.XmlDataDocument
        MyXpath = TextBox2.Text 'This = //Customer
        MyXMLDocument = New System.Xml.XmlDataDocument()
        TextBox1.Text = MyXMLDocument.InnerXml

        Dim MyNameSpace As XmlNamespaceManager = New XmlNamespaceManager(MyXMLDocument.NameTable)
        MyNameSpace.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
        MyNameSpace.AddNamespace("amnet", "http://au.com.amnet.memberutils/")

        Dim MyNode As System.Xml.XmlNode
        MyNode = MyXMLDocument.SelectSingleNode(MyXpath, MyNameSpace)

        If (IsDBNull(MyNode)) Then
            TextBox1.Text = MyNode.Name
            TextBox1.Text = "Null" & TextBox1.Text
        End If

In the end, I got around this by removing all namespaces from the XML document. I still don't completely understand how to manage namespaces in XML.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.