asp.net读取Xml返回IList集合

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;

    }

 

你可能感兴趣的:(asp.net)