Read XML from c#

Viewed 15077

I'm trying to read a xml file from a c# application. so far no luck at all. This is the XML file

<?xml version="1.0" encoding="utf-8"?>
<ExportJobs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <JobList>
    <Job Id="555555">
      <Comments></Comments>
      <DueDate>2017-11-17</DueDate>
      <FormattedDueDate>17-Nov-2017 12:00</FormattedDueDate>
      <TargetDueDate>2017-11-17</TargetDueDate>
      <ServiceTypeId>3</ServiceTypeId>
      <ServiceType>Service</ServiceType>
      <TenantName>Miss Ash</TenantName>
      <Uprn>testUpr</Uprn>
      <HouseName></HouseName>
    </Job>
    <Job Id="666666">
      <Comments></Comments>
      <DueDate>2018-03-15</DueDate>
      <FormattedDueDate>15-Mar-2018 12:00</FormattedDueDate>
      <TargetDueDate>2018-03-15</TargetDueDate>
      <ServiceTypeId>3</ServiceTypeId>
      <ServiceType>Service</ServiceType>
      <TenantName>Mr Howard</TenantName>
      <Uprn>testUpr2</Uprn>
    </Job>
  </JobList>
</ExportJobs>

I'm trying to get the job Id and the Uprn from the joblist node and pass the values in to Sql Server DB. I tried this, but I can't get the values,

            string costCode;
            string uprn;

            //File path where the xml is located
            string filepath = "C:\\ExportJobs.xml";

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);

            foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
            {

                costCode = node.Attributes["Id"].InnerText;
                uprn = node.Attributes["Uprn"].InnerText;
            }

I really appreciate any help. Thanks

4 Answers
Related