日期:2014-05-17  浏览次数:20604 次

操作XML 和asp.net绑定到list
现在我有一个这样的XML文件
<?xml version="1.0" encoding="utf-8"?>
<Car>
  <carcost>
    <ID>20130821133126</ID>
    <uptime>60</uptime>
    <downtime>30</downtime>
    <price>0.4</price>
  </carcost>
  <carcost>
    <ID>20130821014316</ID>
    <uptime>120</uptime>
    <downtime>60</downtime>
    <price>0.3</price>
  </carcost>
  <carcost>
    <ID>20130822043127</ID>
    <uptime>30</uptime>
    <downtime>0</downtime>
    <price>0.5</price>
  </carcost>
  <carcost>
    <ID>20130822043341</ID>
    <uptime>120以上!</uptime>
    <downtime>120</downtime>
    <price>0.2</price>
  </carcost>
</Car>

也有一个实体类 price

现在想把XML里面的数据读取出来

XElement root = XElement.Load(Server.MapPath("CarMoney.xml"));
            IEnumerable<XElement> els = root.Element("carcost").Elements();

            this.repCarMoney.DataSource = lstPrice;
            this.repCarMoney.DataBind();

            foreach (XElement el in els)
            {
                Price price = new Price();
                price.id = el.attribute("id").value;
                price.StartTime = int.Parse(el.Attribute("uptime").Value);
                price.EndTime = int.Parse(el.Attribute("downtime").Value);
                price.Money = int.Parse(el.Attribute("price").Value);
                lstPrice.Add(price);