需求如下,
项目需要将一段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>>的数据结构
我尝试使用了三种方法,实现这个需求
- 使用Regex正则表达式匹配
- 使用XElement类解析
- 使用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); } } } } }