需求如下,

项目需要将一段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>>的数据结构

我尝试使用了三种方法,实现这个需求

  1. 使用Regex正则表达式匹配
  2. 使用XElement类解析
  3. 使用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的方法的更多相关文章

  1. Qt中三种解析xml的方式

    在下面的随笔中,我会根据xml的结构,给出Qt中解析这个xml的三种方式的代码.虽然,这个代码时通过调用Qt的函数实现的,但是,很多开源的C++解析xml的库,甚至很多其他语言解析xml的库,都和下面 ...

  2. Java Web开发Tomcat中三种部署项目的方法

    第一种方法:在tomcat中的conf目录中,在server.xml中的,<host/>节点中添加: <Context path="/hello" docBase ...

  3. 三种读写XML的方法

    XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖于内 ...

  4. Eclipse中三种设置编码格式的方法

    转自:https://blog.csdn.net/rainy_black_dog/article/details/52403735 很早以前听过一位老师说过:咱们中国人不管学习哪种编程语言,总会遇到乱 ...

  5. Android 中三种启用线程的方法

    在多线程编程这块,我们经常要使用Handler(处理),Thread(线程)和Runnable这三个类,那么他们之间的关系你是否弄清楚了呢? 首先说明Android的CPU分配的最小单元是线程,Han ...

  6. Android平台中实现对XML的三种解析方式

    本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能. 在 ...

  7. Notepad++中Windows,Unix,Mac三种格式

    Notepad++中Windows,Unix,Mac三种格式之间的转换 http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htm ...

  8. 【转】Notepad++中Windows,Unix,Mac三种格式之间的转换

    原文网址:http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htmls/npp_func_windows_unix_mac.ht ...

  9. [教程]Delphi 中三种回调函数形式解析

    Delphi 支持三种形式的回调函数 全局函数这种方式几乎是所有的语言都支持的,类的静态函数也可以归为此类,它保存的只是一个函数的代码起始地址指针( Pointer ).在 Delphi 中声明一般为 ...

随机推荐

  1. jquery 的ready() 与window.onload()的区别

    做web开发时常用Jquery中$(document).ready()和JavaScript中的window.onload方法,两者都是要在页面加载完成以后加载的方法,但是这两者还是有很大区别的.最近 ...

  2. cf493C Vasya and Basketball

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. HDU 1402 A * B Problem Plus (FFT模板题)

    FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...

  4. SelectSort 选择排序

    //SelectSort (( O(n²))) public class TestSelectSort { public int[] selectSortArray(int[] arr){ int m ...

  5. python lcd 时间显示

    #!/usr/bin/python # QStopWatch -- a very simple stop watch # Copyright (C) 2006 Dominic Battre <d ...

  6. [原创作品]html css改变浏览器选择文字的背景和颜色

    又很久没有'剥壳'了,最近在为一家公司做一个生产管理解决方案.所以都很忙.今天的话题很简单,就做一个很简单的网页特效.今天偶然浏览到一个网站,他们在选择文字时,样子不是蓝背景和白色字体那么单调,感觉这 ...

  7. Redhat6.4 配置本地网络的FTP YUM源

    Redhat6.4 配置本地网络的FTP YUM源 如果本机IP: 192.168.8.47 (一) 配置本机的yum源 使用以下的方法能够配置本机的yum源: 1) scp命令上传ISO文件到: / ...

  8. JavaScript运算符有哪些

    JavaScript中的运算符有很多,主要分为算术运算符,等同全同运算符,比较运算符,字符串运算符,逻辑运算符,赋值运算符等.这些运算符都有一些属于自己的运算规则,下面就为大家介绍一下JavaScri ...

  9. MVC4 成员资格、 身份验证

    SimpleMembership,成员资格提供程序. 通用的提供者和新的 ASP.NET 4.5 Web 窗体和 ASP.NET MVC 4 模板 ASP.NET MVC 4 互联网模板中添加一些新的 ...

  10. Oracle—RMAN备份(一)

    一.RMAN备份相关概念 1.RMAN备份中表空间不需要处于backup模式下,它备份数据文件,归档日志文件,控制文件,spfile和备份集片,但不备份联机重做日志文件,临时文件和口令文件. 2.备份 ...