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>

 

posted @ 2014-09-28 12:07  talentzemin  阅读(199)  评论(0编辑  收藏  举报