windows phone中三种解析XML的方法
需求如下,
项目需要将一段xml字符串中的信息提取出来
<?xml version=""1.0"" encoding=""UTF-8"" ?>
<root>
<responseHeader>
<sequence>1397045886045</sequence>
<timestamp>2014-04-09 20:12:29</timestamp>
<version>1.0.0</version>
<returnCode>0</returnCode>
<errorMessage>成功</errorMessage>
</responseHeader>
<responsePage>
<hasNextPage>false</hasNextPage>
<hasPreviousPage>false</hasPreviousPage>
<lastPageNumber>1</lastPageNumber>
<totalCount>7</totalCount>
<thisPageFirstElementNumber>1</thisPageFirstElementNumber>
<thisPageLastElementNumber>7</thisPageLastElementNumber>
<nextPageNumber>2</nextPageNumber>
<previousPageNumber>0</previousPageNumber>
<pageSize>20</pageSize>
<thisPageNumber>1</thisPageNumber>
<firstResult>0</firstResult>
</responsePage>
<hotpointInfoList>
<hotpointInfo>
<name>南京北极阁一号</name>
<nasid>360721</nasid>
<address>北极山庄1号</address>
<province>250</province>
<cityCode>320100</cityCode>
<type>99</type>
<longitude>118.7869</longitude>
<latitude>32.0616</latitude>
<isRecommend>0</isRecommend>
</hotpointInfo>
<hotpointInfo>
<name>南京仙林大成名店动感地带旗舰营业厅</name>
<nasid>276495</nasid>
<address>仙林大成名店一层</address>
<province>250</province>
<cityCode>320100</cityCode>
<type>99</type>
<longitude>118.9169</longitude>
<latitude>32.1037</latitude>
<isRecommend>0</isRecommend>
</hotpointInfo>
<hotpointInfo>
<name>南京工业职业技术学院</name>
<nasid>373703</nasid>
<address>南京市羊山路一号</address>
<province>250</province>
<cityCode>320100</cityCode>
<type>99</type>
<longitude>118.9319</longitude>
<latitude>32.1241</latitude>
<isRecommend>0</isRecommend>
</hotpointInfo>
<hotpointInfo>
<name>南京星巴克凤凰书城店</name>
<nasid>257778</nasid>
<address>湖南路1号凤凰国际书城一层B铺位</address>
<province>250</province>
<cityCode>320100</cityCode>
<type>99</type>
<longitude>118.7785</longitude>
<latitude>32.0705</latitude>
<isRecommend>0</isRecommend>
</hotpointInfo>
</root>
将所有的<hotpointInfo> Node 集合取出来
构成List<Dictionary<string,string>>的数据结构
我尝试使用了三种方法,实现这个需求
- 使用Regex正则表达式匹配
- 使用XElement类解析
- 使用XmlReader类解析
使用Regex正则表达式匹配
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
MatchCollection hotspotMatches = Regex.Matches(s,
"<hotpointInfo>.*?<name>(?<name>.*?)</name>.*?<address>(?<address>.*?)</address>.*?<cityCode>(?<city>.*?)</cityCode>.*?<type>(?<hottype>.*?)</type>.*?<longitude>(?<longitude>.*?)</longitude>.*?<latitude>(?<latitude>.*?)</latitude>.*?(.*?<coverageArea>(?<coverarea>.*?)</coverageArea>)?.*?</hotpointInfo>",
RegexOptions.Singleline);
foreach (Match hotspotMatch in hotspotMatches)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["name"] = hotspotMatch.Groups["name"].Value;
dictionary["nasid"] = hotspotMatch.Groups["nasid"].Value;
dictionary["address"] = hotspotMatch.Groups["address"].Value;
dictionary["province"] = hotspotMatch.Groups["province"].Value;
dictionary["cityCode"] = hotspotMatch.Groups["cityCode"].Value;
dictionary["type"] = hotspotMatch.Groups["type"].Value;
dictionary["longitude"] = hotspotMatch.Groups["longitude"].Value;
dictionary["latitude"] = hotspotMatch.Groups["latitude"].Value;
dictionary["isRecommend"] = hotspotMatch.Groups["isRecommend"].Value; list.Add(dictionary);
}
使用XElement类解析
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
XElement xElementFromString = XElement.Parse(s); var v = from hotpointInfoElement in xElementFromString.Element("hotpointInfoList").Elements("hotpointInfo")
select hotpointInfoElement; foreach (XElement xElement in v)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["name"] = xElement.Element("name").Value;
dictionary["nasid"] = xElement.Element("nasid").Value;
dictionary["address"] = xElement.Element("address").Value;
dictionary["province"] = xElement.Element("province").Value;
dictionary["cityCode"] = xElement.Element("cityCode").Value;
dictionary["type"] = xElement.Element("type").Value;
dictionary["longitude"] = xElement.Element("longitude").Value;
dictionary["latitude"] = xElement.Element("latitude").Value;
dictionary["isRecommend"] = xElement.Element("isRecommend").Value; list.Add(dictionary);
}
使用XmlReader类解析
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
using (XmlReader reader = XmlReader.Create(new StringReader(s)))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "hotpointInfo")
{
XElement el = XNode.ReadFrom(reader) as XElement;
if (el != null)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary["name"] = el.Element("name").Value;
dictionary["nasid"] = el.Element("nasid").Value;
dictionary["address"] = el.Element("address").Value;
dictionary["province"] = el.Element("province").Value;
dictionary["cityCode"] = el.Element("cityCode").Value;
dictionary["type"] = el.Element("type").Value;
dictionary["longitude"] = el.Element("longitude").Value;
dictionary["latitude"] = el.Element("latitude").Value;
dictionary["isRecommend"] = el.Element("isRecommend").Value;
list.Add(dictionary);
}
}
}
}
}
windows phone中三种解析XML的方法的更多相关文章
- Qt中三种解析xml的方式
在下面的随笔中,我会根据xml的结构,给出Qt中解析这个xml的三种方式的代码.虽然,这个代码时通过调用Qt的函数实现的,但是,很多开源的C++解析xml的库,甚至很多其他语言解析xml的库,都和下面 ...
- Java Web开发Tomcat中三种部署项目的方法
第一种方法:在tomcat中的conf目录中,在server.xml中的,<host/>节点中添加: <Context path="/hello" docBase ...
- 三种读写XML的方法
XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖于内 ...
- Eclipse中三种设置编码格式的方法
转自:https://blog.csdn.net/rainy_black_dog/article/details/52403735 很早以前听过一位老师说过:咱们中国人不管学习哪种编程语言,总会遇到乱 ...
- Android 中三种启用线程的方法
在多线程编程这块,我们经常要使用Handler(处理),Thread(线程)和Runnable这三个类,那么他们之间的关系你是否弄清楚了呢? 首先说明Android的CPU分配的最小单元是线程,Han ...
- Android平台中实现对XML的三种解析方式
本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能. 在 ...
- Notepad++中Windows,Unix,Mac三种格式
Notepad++中Windows,Unix,Mac三种格式之间的转换 http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htm ...
- 【转】Notepad++中Windows,Unix,Mac三种格式之间的转换
原文网址:http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htmls/npp_func_windows_unix_mac.ht ...
- [教程]Delphi 中三种回调函数形式解析
Delphi 支持三种形式的回调函数 全局函数这种方式几乎是所有的语言都支持的,类的静态函数也可以归为此类,它保存的只是一个函数的代码起始地址指针( Pointer ).在 Delphi 中声明一般为 ...
随机推荐
- [置顶] 顿悟JAVA,自己实现Object的Clone的约束关系(上)
因protected 的理解,顿悟一些JAVA的原理,模拟了Object类的子类为什么在调用clone方法前实现Cloneable接口. 这里不解释 ,上代码先. 运行效果 文件结构 调用类 pack ...
- php之手机号码查归属地
免费手机号码归属地API查询接口 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=1585078144 ...
- iOS 原生二维码扫描,带扫描框和扫描过程动画
在代码中使用了相对布局框架Masonry 准备两张图片,一张是扫描边框,一张是扫描时的细线分别命名 scanFrame.png和scanLine.png并提前放入工程 导入相对布局头文件 #defin ...
- HttpServletRequest对象方法的用法(转)
原文地址:http://blog.csdn.net/xh16319/article/details/8450715 原文作者:ITSTAR 文章太赞,忍不住就想转..... 1. 获得客户机信息 ...
- (转)iOS Wow体验 - 第七章 - 操作图例与触屏人机工学
本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第七章译文精选,其余章节将陆续放出.上一篇:Wow ...
- HDU 2025 查找最大元素
查找最大元素 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- Operation System - Peterson's Solution算法 解决多线程冲突
Person's solution 是用来一种基于软件的解决关键区域问题的算法(critical-section). 它并不是完美的,有可能不对地工作.并且是限制解决两个进程同步的问题. 可是它非常e ...
- 使用代码自定义UIView注意一二三
文/CoderAO(简书作者)原文链接:http://www.jianshu.com/p/68b383b129f9著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 当一撮样式一样的视图在 ...
- .net中将DataTable导出到word、Excel、txt、htm的方法
dt:DataTable strFile:fileName strExt:type private void GridExport(DataTable dt, string strFile, stri ...
- Caused by: java.lang.ClassNotFoundException: javassist.ClassPool
1.错误原因 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...