博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
windows phone中三种解析XML的方法
阅读量:4567 次
发布时间:2019-06-08

本文共 5208 字,大约阅读时间需要 17 分钟。

需求如下,

项目需要将一段xml字符串中的信息提取出来

1397045886045
2014-04-09 20:12:29
1.0.0
0
成功
false
false
1
7
1
7
2
0
20
1
0
南京北极阁一号
360721
北极山庄1号
250
320100
99
118.7869
32.0616
0
南京仙林大成名店动感地带旗舰营业厅
276495
仙林大成名店一层
250
320100
99
118.9169
32.1037
0
南京工业职业技术学院
373703
南京市羊山路一号
250
320100
99
118.9319
32.1241
0
南京星巴克凤凰书城店
257778
湖南路1号凤凰国际书城一层B铺位
250
320100
99
118.7785
32.0705
0
将所有的<hotpointInfo> Node 集合取出来

构成List<Dictionary<string,string>>的数据结构

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

  1. 使用Regex正则表达式匹配
  2. 使用XElement类解析
  3. 使用XmlReader类解析

使用Regex正则表达式匹配

List
> list = new List
>(); MatchCollection hotspotMatches = Regex.Matches(s, "
.*?
(?
.*?)
.*?
(?
.*?)
.*?
(?
.*?)
.*?
(?
.*?)
.*?
(?
.*?)
.*?
(?
.*?)
.*?(.*?
(?
.*?)
)?.*?", RegexOptions.Singleline); foreach (Match hotspotMatch in hotspotMatches) { Dictionary
dictionary = new Dictionary
(); 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
> list = new List
>(); XElement xElementFromString = XElement.Parse(s); var v = from hotpointInfoElement in xElementFromString.Element("hotpointInfoList").Elements("hotpointInfo") select hotpointInfoElement; foreach (XElement xElement in v) { Dictionary
dictionary = new Dictionary
(); 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
> list = new List
>(); 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
dictionary = new Dictionary
(); 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); } } } } }

转载于:https://www.cnblogs.com/fifa0329/p/4536651.html

你可能感兴趣的文章
nginx首页根据IP跳转
查看>>
【2019-08-20】有点目标,有点计划,有点目的
查看>>
【2019-09-10】美,真的跟年龄无关
查看>>
【2019-09-28】少,但更好
查看>>
【2019-09-13】耐心观察是一种技能
查看>>
mysql数据库2-常用命令
查看>>
安卓开发环境搭建(转)
查看>>
Harris角点检测
查看>>
Struts2的处理流程及为Action的属性注入值
查看>>
设计中最常用的CSS选择器
查看>>
Maven项目打包成可执行Jar文件
查看>>
nginx http proxy 正向代理
查看>>
对BFC的总结
查看>>
23醒
查看>>
win7每天出现taskeng.exe进程的解决方案
查看>>
React Children
查看>>
大数据等最核心的关键技术:32个算法
查看>>
Maven多模块项目搭建
查看>>
redis列表list
查看>>
雷林鹏分享: C# 简介
查看>>