2017-07-18 11 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> 

私はジョブIDを取得しようとしていると、ジョブリストノードからUprnで、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

BTW:Visual Studioの "Edit" => "Paste Special" => "Past XML As Classes"メニューオプションがありますが、なぜ私が通常しないのか分かります...(上記のコードで生成されたコードと上記のコードを比較してください) –

+0

ありがとうございます。私はこれをテストします。 – KMR

2

あなたは順番に属性IdUprnが含まれていないだけでJobs要素を含むルート要素のChildNodesにアクセスしています。

通常の実施には、以下のよう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クラスだと思います。

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(); 

     } 
    } 
}