0

I have a project that keeps track of people in XML. Example below:

<?xml version="1.0" encoding="utf-8"?>
<AddressBook>
  <Person>
    <FirstName>Bobby</FirstName>
    <LastName>Hill</LastName>
    <PhoneNumber>0123456789</PhoneNumber>
    <Address>123 Fake St</Address>
  </Person>
  <Person>
    <FirstName>Dangit</FirstName>
    <LastName>Bobby</LastName>
    <PhoneNumber>09088898898</PhoneNumber>
    <Address>Pro-pain</Address>
  </Person>
</AddressBook>

My problem is using reader.ReadStartElement("FirstName");, reader.ReadElementContentAsString("FirstName", "");, or an if statement on reader.Name does not seem to find the <FirstName> or other tags. Example code below:

Person person = new Person();

// Switch statement, doesn't find the "FirstName" tag
switch(reader.ReadStartElement())
{
    case "FirstName":
        if (reader.Read())
        {
            person.FirstName = reader.Value.Trim();
        }
        break;
}

// ReadStartElement, doesn't find the "FirstName" tag
reader.ReadStartElement("FirstName");
person.FirstName = reader.ReadElementContentAsString();
reader.ReadEndElement();

// ReadElementAsString(), doesn't find the "FirstName" tag
person.FirstName = reader.ReadElementContentAsString("FirstName", "");

Could someone give me a bit of guidance into what is going on with my code? The FirstName tags are being created with WriteElementString so I'm not sure why ReadElementAsString isn't finding it.

Tamachan87
  • 277
  • 5
  • 11

0 Answers0