需求如下,

项目需要将一段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. java.lang.ClassNotFoundException错误原因汇总

    开发java很长时间了,还经常会遇到java.lang.ClassNotFoundException这样的错误,最近又处理了一次,起初怀疑是jdk版本比class文件的编译版本低了导致了,但是运维人员 ...

  2. poj3708:函数式化简+高精度进制转换+同余方程组

    题目大意 给定一个函数 找出满足条件   等于 k 的最小的x m,k,d已知 其中 m,k 很大需要使用高精度存储 思路: 对 函数f(m)进行化简 ,令t=ceil( log(d,m) ) 可以得 ...

  3. Best Cow Line (POJ 3617)

    题目: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作. ·从S的头部删除一个字符,加到T的尾部 ·从S的尾部删除一个字符,加到T的尾部 目标是要构 ...

  4. SRM 597DIV1

    250: 首先先特判答案不存在的情况. 再设答案为k,则B[k+1,n]是A的一个子序列,所以, 做法1,枚举k检查子序列是否成立; 做法2,反过来想,从后往前看,最长的一个子序列对应了最小答案. 6 ...

  5. 【hihoCoder第十五周】最近公共祖先·二

    老实说我没有读题,看见标题直接就写了,毕竟hiho上面都是裸的算法演练. 大概看了下输入输出,套着bin神的模板,做了个正反map映射,但是怎么都得不了满分.等这周结束后,找高人询问下trick. 若 ...

  6. jQuery 各种选择器 $.()用法

    jQuery 元素选择器jQuery 使用 CSS 选择器来选取 HTML 元素. $("p") 选取 <p> 元素. $("p.intro") 选 ...

  7. <php>统计目录数和文件数

    $dirn = 0; //目录数 $filen = 0; //文件数 //用来统计一个目录下的文件和目录的个数 function getdirnum($file) { global $dirn; gl ...

  8. 最简单的内核模块hello world

    [root@rt2m09617.sqa.tbc /home/ahao.mah/main] #cat hello.c // Defining __KERNEL__ and MODULE allows u ...

  9. 集成Dubbo服务(Spring)

    Dubbo是什么? Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点. Dubbo[]是 ...

  10. kendo ui中grid页面参数问题

    kendo ui 中grid 算是最长用的控件之一,当使用分页效果时,我们要传递分页参数与自己定义的参数如: var dataSource = new kendo.data.DataSource({ ...