Computer
Computer

Reputation: 2227

Read node from XML file

A little confused with what im doing wrong here. My XML

<?xml version="1.0" encoding="utf-8"?>
<customers xmlns="http://example.com/ns/" si="0" records="2">
  <customer id="123456789">
    <dob>2017-12-10T16:22:27.033Z</dob>
    <location>
      <number>444555666777</number>
    </location>
    <link rel="self" href="http://example.com" />
  </customer>
  <customer id="987654321">
    <dob>2017-12-11T17:00:00.033Z</dob>
    <location>
      <number>555666999888</number>
    </location>
    <link rel="self" href="http://example.com" />
  </customer>
  <link rel="self" href="http://example.com" />
</customers>

I'm trying to get the two records (customer) along with the data contained within them in this instance from a file (eventually i will change this to LoadXml as the XML targeted is from a URL but for testing i have copied into a file to work offline)

So my code

    Dim Xd As XmlDocument = New XmlDocument
    Xd.Load("C:\XMLFile1.xml")
    Dim Total As Integer = Xd.DocumentElement.Attributes("records").Value

    If Total > 0 Then
    Dim Customer = Xd.SelectSingleNode("/customers/customer")

Customer is always nothing? I have tried several variations including

Xd.SelectSingleNode("/customers")
Xd.SelectSingleNode("/customer")

I tried Xd.DocumentElement but i dont think this is correct either.

Where am i going wrong? Searched google and many examples are using the same as above?

Edit

I decided to look at Linq to XML and this is what i have

    Dim X As XElement = Xelement.Load("C:\XMLFile1.xml")
    Dim Total = Xelement.Attribute("records").Value
    Dim Customers As IEnumerable(Of XElement) = Xelement.Elements()

    ' Read the entire XML
    For Each c In Customers
        Console.WriteLine(c)
    Next

Looks good but i dont know if theres anything else i need to add or if theres a better way.

Upvotes: 1

Views: 88

Answers (1)

Cinchoo
Cinchoo

Reputation: 6326

Here is one way of loading the xml into objects

First define objects matching with xml format

[XmlRoot("customers", Namespace = "http://example.com/ns/")]
public class Customers
{
    [XmlAttribute("records")]
    public int Records { get; set; }

    [XmlElement("customer")]
    public Customer[] CustomerArray;
}

public class Customer
{
    [XmlAttribute("id")]
    public int Id { get; set; }

    [XmlElement("dob")]
    public DateTime Dob { get; set; }

    [XmlElement("location")]
    public Location Location { get; set; }

    [XmlElement("link")]
    public Link Link { get; set; }
}

public class Location
{
    [XmlElement("number")]
    public long Number { get; set; }
}

public class Link
{
    [XmlAttribute("rel")]
    public string Rel { get; set; }
    [XmlAttribute("href")]
    public string HRef { get; set; }
}

Then deserialize is as below

System.Xml.Serialization.XmlSerializer serializer11 = new System.Xml.Serialization.XmlSerializer(typeof(Customers));
Customers obj11 = (Customers)serializer11.Deserialize(XElement.Load(/* YOUR XML FILE PATH */).CreateReader());

Hope it helps.

Upvotes: 1

Related Questions