现在我有一个这样的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
<?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
http://www.cnblogs.com/insus/p/3277595.html另外一点,需要注意一下,就是区分大小写。如果找不到节点,当然会出现异常了。
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.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
恩 我今天写出来了。。不过还是谢谢你,现在有一个不是问题的问题,就是我把这些数据读出来之后放在了list<CarCost> lst=new list<carCost>();里面
有一个功能就是可以根据lst[i].ID来进行修改,我想问下,怎么修改lst,我忘记了。也没有找到这样的属性或者方法。。
恩 我今天写出来了。。不过还是谢谢你,现在有一个不是问题的问题,就是我把这些数据读出来之后放在了list<CarCost> lst=new list<carCost>();里面
有一个功能就是可以根据lst[i].ID来进行修改,我想问下,怎么修改lst,我忘记了。也没有找到这样的属性或者方法。。
有一个功能就是可以根据lst[i].ID来进行修改,我想问下,怎么修改lst,我忘记了。也没有找到这样的属性或者方法。。
下面这个链接是前者:
http://www.cnblogs.com/insus/p/3276691.html
CarCost ss = resultList.Single(x => x.ID == "20130822043341");
ss.Price = 220;//直接给ID为20130822043341的对象的Price属性赋值就可以了。resultList中ID为20130822043341的对象的price属性就已经修改好了。