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/">
  <Period>
    <Summary xmlns="http://amcom.com.au/UsageWebServices">
      <Customer>48413</Customer>
      <Start>2010-07-19T00:00:00</Start>
      <Finish>2010-08-19T00:00:00</Finish>
      <PeakOtherOctetsIn>10543479372</PeakOtherOctetsIn>
      <PeakOtherOctetsOut>2469179785</PeakOtherOctetsOut>
      <PeakPeerOctetsIn>963088543</PeakPeerOctetsIn>
      <PeakPeerOctetsOut>51151688</PeakPeerOctetsOut>
      <OffpeakOtherOctetsIn>6651739048</OffpeakOtherOctetsIn>
      <OffpeakOtherOctetsOut>1489804526</OffpeakOtherOctetsOut>
      <OffpeakPeerOctetsIn>319362717</OffpeakPeerOctetsIn>
      <OffpeakPeerOctetsOut>39202998</OffpeakPeerOctetsOut>
    </Summary>
    <RateLimits xmlns="http://amcom.com.au/UsageWebServices" />
  </Period>
  <AllowanceList>
    <RateGroupName xmlns="http://amcom.com.au/UsageWebServices">ADSL 2+ Enabled 45G/65G - $49.00</RateGroupName>
    <Allowances xmlns="http://amcom.com.au/UsageWebServices">
      <Allowance>
        <Name>Off-Peak Basic</Name>
        <OctetsIn>65000000000</OctetsIn>
        <OctetsOut xsi:nil="true" />
        <Period>Offpeak</Period>
        <Class>Other</Class>
      </Allowance>
      <Allowance>
        <Name>Off-Peak Peering</Name>
        <OctetsIn>130000000000</OctetsIn>
        <OctetsOut xsi:nil="true" />
        <Period>Offpeak</Period>
        <Class>Peer</Class>
      </Allowance>
      <Allowance>
        <Name>Peak Basic</Name>
        <OctetsIn>45000000000</OctetsIn>
        <OctetsOut xsi:nil="true" />
        <Period>Peak</Period>
        <Class>Other</Class>
      </Allowance>
      <Allowance>
        <Name>Peak Peering</Name>
        <OctetsIn>90000000000</OctetsIn>
        <OctetsOut xsi:nil="true" />
        <Period>Peak</Period>
        <Class>Peer</Class>
      </Allowance>
    </Allowances>
  </AllowanceList>
</CurrentPeakUsage>

My code contains:

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

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()
        MyXMLDocument.Load("c:\data.xml")
        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
        Else
            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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.