需求如下,
项目需要将一段xml字符串中的信息提取出来
将所有的<hotpointInfo> Node 集合取出来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
构成List<Dictionary<string,string>>的数据结构
我尝试使用了三种方法,实现这个需求
- 使用Regex正则表达式匹配
- 使用XElement类解析
- 使用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); } } } } }