首页 > 代码库 > C#读取XML
C#读取XML
/// <summary> ///读取xml的方法 /// </summary> /// <returns></returns> public List<People> GetXml() { List<People> list = new List<People>(); XmlDocument xmldoc = new XmlDocument(); //获取物理路径 string path = Server.MapPath("/Content/user.xml"); //加载xml文档 xmldoc.Load(path); //第一个节点 XmlNode xmlnode = xmldoc.SelectSingleNode("CATALOG"); //遍历子节点 foreach (XmlNode node in xmlnode) { list.Add(new People()
{ Name = node.ChildNodes[0].InnerText,
Sex = node.ChildNodes[1].InnerText,
Age = Convert.ToInt32(node.ChildNodes[2].InnerText),
Description = node.ChildNodes[3].InnerText
}); } return list; }
<?xml version="1.0" encoding="utf-8"?><CATALOG> <people> <name>wuboo</name> <sex>man</sex> <age>21</age> <description>You’re more than a shadow, I’ve just to believe.</description> </people> <people> <name>laqi</name> <sex>man</sex> <age>24</age> <description>Remember, always laugh when you can. It‘s cheaper than medicine. </description> </people> <people> <name>masa</name> <sex>woman</sex> <age>23</age> <description>From now on, I will expect nothing, and just take what I get.</description> </people></CATALOG>
C#读取XML
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。