从 C# 读取 XML

KMR*_*KMR 3 c# xml xml-parsing

我正在尝试从 ac# 应用程序读取 xml 文件。到目前为止,根本没有运气。这是 XML 文件

<?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>
Run Code Online (Sandbox Code Playgroud)

我正在尝试从作业列表节点获取作业 IDUprn,并将值传递给 Sql Server DB。我试过这个,但我无法得到值,

            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;
            }
Run Code Online (Sandbox Code Playgroud)

我真的很感激任何帮助。谢谢

Mar*_*ell 5

XmlSerializer 是你的朋友:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

public class ExportJobs
{
    public List<Job> JobList { get; } = new List<Job>();
}
public class Job
{
    [XmlAttribute]
    public int Id { get; set; }
    public string Comments { get; set; }
    public DateTime DueDate { get; set; }
    public string FormattedDueDate { get; set; }
    public DateTime TargetDueDate{ get; set; }
    public int ServiceTypeId { get; set; }
    public string ServiceType { get; set; }
    public string TenantName { get; set; }
    public string Uprn { get; set; }
    public string HouseName { get; set; }
}
static class P
{

    static void Main()
    {
        var ser = new XmlSerializer(typeof(ExportJobs));
        ExportJobs jobs;
        using (var sr = new StringReader(xml))
        {
            jobs = (ExportJobs) ser.Deserialize(sr);
        }

        foreach(var job in jobs.JobList)
        {
            Console.WriteLine($"{job.Id} / {job.Uprn}: {job.DueDate}");
        }  
    }

    const string xml = @"<?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>";
}
Run Code Online (Sandbox Code Playgroud)