C# XMLDocument解析XML归纳
来源:互联网 发布:ubuntu桌面版 编辑:程序博客网 时间:2024/05/22 17:35
手机端结束之后的网络版依旧需要解析XML,这里做一个使用的归纳,不包含任何源码分析。
导入名空间:
vs需要在project中添加新的引用,选择System.XML,然后在代码中添加using System.XML;和using System.IO.;
加载文件或者字符串
XmlDocument doc = new XmlDocument(); doc.Load(@"D:\ITP点呼\02.Business\App\Transtron.ItpV5.Business.F24.Implement\Business\XMLFile1.xml"); // 如果是string doc.LoadXml(str);
使用绝对路径获取数据内容:
xml使用范例:
<?xml version="1.0" encoding="utf-8" ?> <bookstore> <book> <title>C语言</title> <author name ="XIAO"> <age>25</age> </author> <price>50</price> </book> <book> <title>数据库</title> <price>100</price > </book > <book> <title>数据结构</title> <age>100</age> <author name ="Wang"> <age>25</age> <sex>female</sex> </author> <price>66.5</price > </book ></bookstore>
获取所有的price数据:
XmlElement root = null; root = doc.DocumentElement; XmlNodeList listNodes = null; listNodes = root.SelectNodes("/bookstore/book/price"); String res = ""; foreach (XmlNode node in listNodes) { res += node.InnerText + "\n"; }
以上代码可以选出所有的price元素,当然绝对路径还有一些别的使用技巧,参考:http://www.cnblogs.com/RiseSoft/archive/2012/03/17/2404007.html
但是如果获取组数据,最好是一个对象一个对象完整的获取,所以可以使用以下方式:
xml使用范例:
<?xml version="1.0" encoding="utf-8" ?> <Results> <Result> <ForcastInfo>陌上花开</ForcastInfo> <ResultInfo>可缓缓归以</ResultInfo> </Result> <Result> <ForcastInfo>故乡遥</ForcastInfo> <ResultInfo>何日去</ResultInfo> </Result> <Result> <ForcastInfo>情不为因果</ForcastInfo> <ResultInfo>缘注定生死</ResultInfo> </Result></Results>
这个xml比较工整。然后获取这个xml的result数组。
List<HealthyInfoModel> resList = new List<HealthyInfoModel>(); XmlNodeList nodeList = doc.SelectNodes("/Results/Result"); if (nodeList != null) { foreach(XmlNode resultNode in nodeList){ XmlNodeList childNodeList = resultNode.ChildNodes; result = new HealthyInfoModel(); foreach (XmlNode node in childNodeList) { if (node.Name.Equals("ForcastInfo")) { String forcastInfo = node.InnerText; result.forcastInfo = forcastInfo; } else if (node.Name.Equals("ResultInfo")) { String resultInfo = node.InnerText; result.resultInfo = resultInfo; } } resList.Add(result); } }以上代码可以完整的获取每一个对象。
参考:http://www.cnblogs.com/kissdodog/archive/2013/02/24/2924236.html
使用XMLDocument是DOM的方式,如果想使用SAX的方式读取,可以使用XMLReader类,这个比DOM节约内存,不过使用起来有一些繁琐。
XML解析的依旧是Result的数组。
XmlReader reader = XmlReader.Create(@"D:\ITP点呼\02.Business\App\Transtron.ItpV5.Business.F24.Implement\Business\XMLFile1.xml"); reader.ReadString(); List<HealthyInfoModel> resList = new List<HealthyInfoModel>(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { String elementName = reader.Name; if (elementName.Equals("Result")) { result = new HealthyInfoModel(); } else if (elementName.Equals("ForcastInfo") && reader.Read()) { String forcastInfo = reader.Value; result.forcastInfo = "[data from XMLReader]:" + forcastInfo; } else if (elementName.Equals("ResultInfo") && reader.Read()) { String resultInfo = reader.Value; result.resultInfo = "[data from XMLReader]:" + resultInfo; } } else if (reader.NodeType == XmlNodeType.EndElement) { String elementName = reader.Name; if (elementName.Equals("Result")) { resList.Add(result); } } }
SAX会一个节点一个节点的读取,并且对节点结束也有响应,可以在指定的EndElement处进行处理,比如,这里在Result的结束也就是遇到xml文件中的</Result>时,想list里面插入完整的HealthyModel对象。
参考:http://blog.csdn.net/yukaizhao/article/details/6618707
0 0
- C# XMLDocument解析XML归纳
- 用XMLDocument解析xml
- C# XML XmlDocument
- C# XML XmlDocument
- DOM解析xml归纳
- XML文件内容解析(使用XmlDocument)
- C#通过XmlDocument操作Xml文档
- C# XML增删改查(XmlDocument)
- C# XMLDocument
- C#操作Xml:通过XmlDocument读写Xml文档
- C#操作Xml:通过XmlDocument读写Xml文档
- C#操作Xml:通过XmlDocument读写Xml文档
- C#操作Xml:通过XmlDocument读写Xml文档
- C#操作Xml:通过XmlDocument读写Xml文档
- C#操作XML的完整例子——XmlDocument篇
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- C#操作XML的完整例子——XmlDocument篇
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- Sublime Text 3 适用于ionic开发的插件下载
- 一些关于css div的注意事项
- OHEM-Training Region-based Object Detectors with Online Hard Example Mining - cvpr 2016 oral
- Android第一行代码学习笔记八----网络技术
- 使用ButterKnife8.4.0的gradle配置
- C# XMLDocument解析XML归纳
- Android NDK学习笔记15-配置AndroidStudio
- 理解OAuth 2.0
- 深度学习框架可使用组件化的思考&BP反向传播的思考
- Android性能优化之一:ViewStub
- ODI环境搭建_04安装Oracle Data Integrator
- ViewPager的使用<一>
- Jquery Datatable 数据填充报错:requested unknown parameter 'XXX' for row xx, column xx 解决方法
- 话一:Wince开发之软硬件需求