需求如下,

项目需要将一段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. NOI2011 兔农

    http://www.lydsy.com/JudgeOnline/problem.php?id=2432 感觉是day1中最难的一题,还好出题人很良心,给了75分部分分. 还是跪拜策爷吧~Orz ht ...

  2. Delphi 调试 通过BreakPoint

    1.打个断点, 如下图 2. 在断点上,邮件,如下图 3. 弹出一个窗体 ,如下图 在 condition 中写条件就可以了.  这样就可以按你假设的条件来进行了,方便.

  3. Codeforces Round #272 (Div. 1) Problem C. Dreamoon and Strings

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. Abstract Methods and Classes

    阅读了Java的官方Doc,在此总结如下. Abstract Class & Method In jave, "abstract" can be a modifier to ...

  5. 必看谷歌HTML/CSS规范

    背景 这篇文章定义了 HTML 和 CSS 的格式和代码规范,旨在提高代码质量和协作效率. 通用样式规范 协议 省略图片.样式.脚本以及其他媒体文件 URL 的协议部分( http:,https: ) ...

  6. (转)iOS7界面设计规范(1) - UI基础 - 为iOS7而设计

    今天开个新坑.其实老早就想做这事儿了.记得前一两年,苹果官方还会在开发者中心提供中文的HIG(Human Interface Guideline),后来给没了:网上能够找到的中文版本不知是官方还是同行 ...

  7. 细说php(六) 数组

    一.数组概述 1.1 数组是复合类型 1.2 数组中能够存储随意长度的数据, 也能够存储随意类型的数据 二.数组的类型 2.1 索引数组: 下标是顺序整数作为索引 <?php $user[0] ...

  8. install samba on crux without net.

    1. 解压文件 tar -xvfz samba-.tar.gz 2. 进入目录 cd samba- 3. 配置 ./configure 4. 编译 make 5. 安装 make install

  9. samba错误

    1.session setup failed: NT_STATUS_LOGON_FAILURE 该错误表示用户有误, 可能是用户不存在, 也有可能是密码错误, 或者用户只是在samba和系统的用户中的 ...

  10. oc随笔六:字典

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...