Xml File 內容 :

<?xml version="1.0" standalone="yes"?>
<table1>
    <Name>Alice</Name>
    <Mail>alice@mail.com</Mail>
    <Address>No. 19-1, Industry E. Road  IV, Hsinchu Science Park, Hsinchu 30077, Taiwan</Address>
</table1>

 

C# 內容 :

//定義的Class

public class ClassA
{
    public string Name { get; set; }
    public string Mail { get; set; }
    public string Address { get; set; }
    public string table1_Id { get; set; }
}

//轉換的程式碼 :

XDocument xDoc = XDocument.Load(XmlFilePath);

IEnumerable<ClassA> table1=
    (from s in xDoc.Descendants("table1")
     select new ClassA()
     {
         Name = s.Element("Name") != null ? s.Element("Name").Value : null,
         Mail = s.Element("Mail") != null ? s.Element("Mail").Value : null,
         Address = s.Element("Address") != null ? s.Element("Address").Value : null,
     }).ToList();

 

結果 :

XmlToClassList.png


arrow
arrow
    文章標籤
    C#
    全站熱搜

    伊 發表在 痞客邦 留言(0) 人氣()