2017-07-18 77 views
1

我正在嘗試從c#應用程序中讀取xml文件。到目前爲止沒有運氣。這是XML文件從c#中讀取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> 

我試圖讓作業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; 
      } 

我真的很感謝任何幫助。由於

+1

'Uprn'是一個元素,而不是屬性;然而,'XmlSerializer'是你的朋友在這裏... –

回答

2

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>"; 
} 
+0

順便說一句:在Visual Studio中有*是*「編輯」=>「選擇性粘貼」=>「過去的XML作爲類」菜單選項,但如果使用它,會明白爲什麼我通常不會......(將生成的代碼與上面的答案中的代碼進行比較) –

+0

非常感謝。我會測試這個。 – KMR

2

您正在訪問的根元素,其中只包含Jobs元素,爲了不包含屬性IdUprnChildNodes

通常的做法是使用XPath查詢如下:

foreach (XmlNode node in xmlDoc.DocumentElement.SelectNodes("Jobs/Job")) 
{ 

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

注意Uprn是節點,而不是節點屬性。

+0

但是,Marc Gravell♦的答案更適用:) –

2

我認爲解決你的問題的最好方法是XDocument類。

XDocument xDoc = XDocument.Load(@"D:\1.xml"); 
    foreach(var node in xDoc.Descendants("Job")) 
    { 
     id = node.Attribute("Id"); 
     foreach(var subnode in node.Descendants("Uprn")) 
     { 
      uprn = subnode.Value; 
     } 

     //or like that. but check it for null before 
     uprn = node.Descendants("Uprn")?.First().Value 
    } 
2

這裏是測試代碼。你需要命名空間。請參閱以下代碼使用xml linq

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication67 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      XElement exportJobs = doc.Root; 
      XNamespace ns = exportJobs.GetDefaultNamespace(); 

      var results = exportJobs.Descendants(ns + "Job").Select(x => new { 
       id = (string)x.Attribute(ns + "Id"), 
       comment = (string)x.Element(ns + "Comments"), 
       dueDate = (DateTime)x.Element(ns + "DueDate"), 
       formattedDueDate = (DateTime)x.Element(ns + "FormattedDueDate"), 
       targetDueDate = (DateTime)x.Element(ns + "TargetDueDate"), 
       serviceTypeId = (int)x.Element(ns + "ServiceTypeId"), 
       serviceType = (string)x.Element(ns + "ServiceType"), 
       tenantName = (string)x.Element(ns + "TenantName"), 
       uprn = (string)x.Element(ns + "Uprn"), 
       houseName = (string)x.Element(ns + "HouseName") 
      }).ToList(); 

     } 
    } 
}