日期:2014-05-18 浏览次数:20856 次
private void InitList() { XmlDocument xml = new XmlDocument(); xml.Load("../../XMLFile1.xml"); XmlNode x = xml.DocumentElement; foreach(XmlNode xx in x.ChildNodes) { tv t = new tv(); t.TvName = xx["tvName"].InnerText; t.TvType = xx["tvType"].InnerText; foreach(XmlNode xxx in xx.ChildNodes) { if (xxx.Name == "programs") { foreach (XmlNode xxxx in xxx.ChildNodes) { String s = xxxx.InnerText; //此处要是使用索引器就会报错 如 s = xxxx["ProgramName"].InnerText; //未将对象引用到实例 ...很费解. t.ProgramName.Add(s); } } } list.Add(t);//存储tv实体类的泛型集合 } }
<?xml version="1.0" encoding="utf-8" ?> <tv> <tvProgram> <tvType>黑白</tvType> <tvName>北京电视台</tvName> <programs> <ProgramName>认错</ProgramName> </programs> </tvProgram> <tvProgram> <tvType>RGB</tvType> <tvName>南京电视台</tvName> <programs> <ProgramName>认错</ProgramName> </programs> </tvProgram> <tvProgram> <tvType>彩色</tvType> <tvName>东京电视台</tvName> <programs> <ProgramName>认错</ProgramName> </programs> </tvProgram> </tv>