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

解析xml

时间:2015-08-25 18:39:49      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

一:把xml文件做成串

string content = @"<xml>
<name>123</name>
<class><time>2015-1-1</time></class>
<class><time>2015-1-1</time></class>
<class><time>2015-1-1</time></class>
<class><time>2015-1-1</time></class>
</xml>
";
XmlDocument doc = new XmlDocument();
doc.LoadXml(content);

XmlNodeList list = doc.GetElementsByTagName("class");

foreach (XmlNode item in list)
{
var aa = item.InnerText; 
}

二:读取文件夹中的xml 文件


// xmlFileName 路径
 if (!File.Exists(xmlFileName)) return null;

                //加载XML
                var xmldoc = new XmlDocument();
                xmldoc.Load(xmlFileName);
                //获取根目录
                var root = xmldoc.DocumentElement;
                if (root == null) return null;
                var nodeCount = root.ChildNodes.Count;
                if (nodeCount <= 0) return null;
                var j = 0;
                list = new List<RecordModel>(0);
                //取值
                for (var i = nodeCount - 1; i >= 0; i--)
                {

                    if (count != -1)
                    {
                        j++;
                        if (j > count) { break; }
                    }
                    var node = root.ChildNodes[i];
                    if (node == null) continue;                 var recordModel = new RecordModel
                        {
                            SendUserId = node["SendUserId"].InnerText,
                            Message = node["Message"].InnerText,
                            Time = node["Time"].InnerText,

                        };
                        var domainnode = node["Domain"];
                        if (domainnode != null)
                        {
                            recordModel.Domain = domainnode.InnerText;
                        }
                        var urlAddressnode = node["UrlAddress"];
                        if (urlAddressnode != null)
                        {
                            recordModel.Domain = urlAddressnode.InnerText;
                        }
                        list.Add(recordModel);

                }
                return list;

 

解析xml

标签:

原文地址:http://www.cnblogs.com/xiaoyaodijun/p/4757940.html

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