码迷,mamicode.com
首页 > 其他好文 > 详细

C#读取XML

时间:2014-09-28 14:21:22      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   for   sp   div   

        /// <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

标签:des   style   blog   color   io   ar   for   sp   div   

原文地址:http://www.cnblogs.com/talentzemin/p/3998033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!