现在我有一个这样的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 xmllistasp.net

解决方案 »

  1.   

    Refer this:
    http://www.cnblogs.com/insus/p/3277595.html另外一点,需要注意一下,就是区分大小写。如果找不到节点,当然会出现异常了。
      

  2.   

    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; }
        }
      

  3.   

      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
      

  4.   


    恩 我今天写出来了。。不过还是谢谢你,现在有一个不是问题的问题,就是我把这些数据读出来之后放在了list<CarCost> lst=new list<carCost>();里面
    有一个功能就是可以根据lst[i].ID来进行修改,我想问下,怎么修改lst,我忘记了。也没有找到这样的属性或者方法。。
      

  5.   


    恩 我今天写出来了。。不过还是谢谢你,现在有一个不是问题的问题,就是我把这些数据读出来之后放在了list<CarCost> lst=new list<carCost>();里面
    有一个功能就是可以根据lst[i].ID来进行修改,我想问下,怎么修改lst,我忘记了。也没有找到这样的属性或者方法。。
      

  6.   

    谢谢你,现在有一个不是问题的问题,就是我把这些数据读出来之后放在了list<CarCost> lst=new list<carCost>();里面
    有一个功能就是可以根据lst[i].ID来进行修改,我想问下,怎么修改lst,我忘记了。也没有找到这样的属性或者方法。。
      

  7.   

    直接修改xml,还是想修改List<T>?
    下面这个链接是前者:
    http://www.cnblogs.com/insus/p/3276691.html
      

  8.   

    修改list,现在界面上只需要操作list。最后在吧list保存到XML里面
      

  9.   

    赋值之后呢,怎么保存,还是list.Add(类);
      

  10.   

    引用我上面最终生成的resultList。
    CarCost ss = resultList.Single(x => x.ID == "20130822043341");
                ss.Price = 220;//直接给ID为20130822043341的对象的Price属性赋值就可以了。resultList中ID为20130822043341的对象的price属性就已经修改好了。
      

  11.   

    恩好的。。我会了。谢谢啊。。在请你帮个忙。。现在这个list里面有一个两个时间,一个是开始的,一个是结束的。我在添加的的时候输入的开始时间不能与list里面原有的时间交叉,意思是说假如我的list里面有1-30;30-60;60-120;现在我做添加的时候,时间不能是1-120这个范围里面的数字。。请问怎么去判断