需求如下,

项目需要将一段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. HIbernate Oracle存储过程

    之前为了实现基于Hibernate+Oracle的存储过程调用,发现了一个又一个坑,然后一个一个的尝试解决. 需求:使用Hibernate调用Oracle的存储过程,需要支持的有动态表名.存储过程变量 ...

  2. Javascript中&&和&,||和|运算符两个不同点

    1.性能上的比较 如果&&的第一个运算数是false,就不再考虑第二个运算数,直接返回false:如 果||的第一个运算数是true,也不再考虑第二个运算数,直接返回true.& ...

  3. C 编程调试集

    gcc rw.c rw.c:75:6: warning: conflicting types for ‘process_conn_server’ void process_conn_server(in ...

  4. PHP常用魔术方法(__set、__get魔术方法:)

    __set.__get魔术方法: //文件名:Object.php <?phpnamespace IMooc;class Object{ protected $array = array(); ...

  5. javaweb文件下载

    最近搞了一下struts文件上传下载了,一个是通过struts自带的类实现的下载方法,一个是通用的下载方法: struts实现: FileDownloadAction.java package com ...

  6. android 点击桌面图标,打开手机浏览器进入对应的站点

    做一个假的adnroid app.要实现点击桌面图标.打开手机浏览器进入对应的站点,实现方法非常easy import android.app.Activity; import android.con ...

  7. LVM管理

    一.步骤: 1.创建新的分区,并修改分区类型为8e 2.创建物理卷PV 3.将新建的PV添加到要扩展的VG中 4.用命令lvextend或lvresize来将新加入的PE添加到要扩展的LV中 5.用命 ...

  8. 从点亮一个LED开始,Cortex-A9裸机程序设计

    电路原理图: 如何点亮一个LED? 通过对原理图进行分析,我们能够发现给三极管的基极加上一个高点平时,三级管be结导通构成通路,此时二极管就点亮了.若要将LED熄灭只需取消高电平输出. 如何使三级管基 ...

  9. 如何判断Linux load的值是否过高

    1.先使用top看下CPU占用高的进程,找出进程的进程ID(pid): 查看方法:top 2.根据进程ID(pid)查看是进程的那些线程占用CPU高. 查看方法:top -Hp pid 3.使用pst ...

  10. 关于Oracle dmp文件导入随笔

    进入博客园已经两年多了,每次想写点什么,都是给自己个各种借口,不了了之~今天就从Oracle数据库最长用的导入开始吧! 1.低版本的exp/imp可以连接到高版本(或同版本)的数据库服务器,比如:10 ...