首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

操作XML 跟asp.net绑定到list

2013-09-06 
操作XML 和asp.net绑定到list现在我有一个这样的XML文件?xml version1.0 encodingutf-8?Carcar

操作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);


            }
他一直给我报一个未将对象设置到对象的实例化 XML?list?asp.net? xml list asp.net
[解决办法]
Refer this:
http://www.cnblogs.com/insus/p/3277595.html

另外一点,需要注意一下,就是区分大小写。如果找不到节点,当然会出现异常了。
[解决办法]
refer:

class Program
    {
        static void Main(string[] args)
        {
            IList<CarCost> resultList = new List<CarCost>();

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("test.xml");

            XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode("Car").ChildNodes;
            foreach (XmlNode list in xmlNodeList)
            {
                CarCost carcost = new CarCost
                (
                    list.SelectSingleNode("ID").InnerText,
                    list.SelectSingleNode("uptime").InnerText,
                    list.SelectSingleNode("downtime").InnerText,
                    float.Parse(list.SelectSingleNode("price").InnerText)


                );
                resultList.Add(carcost);
            }

            IEnumerator enumerator = resultList.GetEnumerator();
            while (enumerator.MoveNext())
            {
                CarCost carCost = enumerator.Current as CarCost;
                Console.WriteLine(carCost.ID + " " + carCost.UpTime + " " + carCost.DownTime + " " + carCost.Price);
            }
        }
    }

    public class CarCost
    {
        public CarCost(string id, string uptime, string downtime, float price)
        {
            this.ID = id;
            this.UpTime = uptime;
            this.DownTime = downtime;
            this.Price = price;
        }
        public string ID { get; set; }
        public string UpTime { get; set; }
        public string DownTime { get; set; }
        public float Price { get; set; }
    }


[解决办法]
  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);
            }
前面就绑定lstPrice给repCarMoney时,还未给lstPrice添加项啊

至于XML读取,参考http://blog.csdn.net/chinajiyong/article/details/7485019

热点排行