asp.net讀取Xml返回IList集合

來源:互聯網
上載者:User

Xml檔案:

View Code

<?xml version="1.0" encoding="UTF-8"?><DirectoryListing>  <Services1>    <Name>基本服務</Name>    <Price>100</Price>    <Date>30</Date>    <Type>1</Type>  </Services1>  <Services2>    <Name>資訊增值服務</Name>    <Price>100</Price>    <Date>30</Date>    <Type>2</Type>  </Services2>  <Services3>    <Name>電話預約委託服務</Name>    <Price>150</Price>    <Date>20</Date>    <Type>3</Type>  </Services3></DirectoryListing>

根據Xml檔案 ,產生實體類:

View Code

   [Serializable]  public  class Serveris    {      public Serveris() { }      public string Name { get;set;}      public decimal Price { get; set; }      public int Date { get; set; }      public int Type { get; set; }     }

.cs檔案實現 方法讀取Xml:

View Code

public IList<Serveris> Xml()    {        IList<Serveris> list = new List<Serveris>();        //建立XmlDocument對象        XmlDocument xmlDoc = new XmlDocument();        string filen = Server.MapPath("~/XMLServices.xml");        //載入xml檔案名稱        xmlDoc.Load(filen);        //讀取根節點的所有子節點,放到xn0中         XmlNodeList xn0 = xmlDoc.SelectSingleNode("DirectoryListing").ChildNodes;        //尋找二級節點的內容或屬性         foreach (XmlElement oon in xn0)        {            Serveris ser = new Serveris();            ser.Name = oon.GetElementsByTagName("Name")[0].InnerText;            ser.Price = Convert.ToDecimal(oon.GetElementsByTagName("Price")[0].InnerText);            ser.Date = Convert.ToInt32(oon.GetElementsByTagName("Date")[0].InnerText);            ser.Type = Convert.ToInt32(oon.GetElementsByTagName("Type")[0].InnerText);            list.Add(ser);        }        return list;    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.