什么是解析lat和long以下xml片段的最简单方法.没有名称空间等.
它是一个字符串变量.不是流.
stockholm sweden 51.1 67.98
到目前为止我所阅读的所有内容都过于复杂,不应该是一项简单的任务,例如 http://geekswithblogs.net/kobush/archive/2006/04/20/75717.aspx
我一直在看上面的链接
当然在.net中有一种更简单的方法可以做到这一点?
使用Linq for XML:
XDocument doc= XDocument.Parse(""); var points=doc.Descendants("gpoint"); foreach (XElement current in points) { Console.WriteLine(current.Element("lat").Value); Console.WriteLine(current.Element("lng").Value); } Console.ReadKey(); stockholm sweden 51.1 67.98
using System.IO; using System.Xml; using System.Xml.XPath;
...
string xml = @""; XmlReaderSettings set = new XmlReaderSettings(); set.ConformanceLevel = ConformanceLevel.Fragment; XPathDocument doc = new XPathDocument(XmlReader.Create(new StringReader(xml), set)); XPathNavigator nav = doc.CreateNavigator(); Console.WriteLine(nav.SelectSingleNode("/poi/gpoint/lat")); Console.WriteLine(nav.SelectSingleNode("/poi/gpoint/lng")); stockholm sweden 51.1 67.98
您当然可以使用xpath SelectSingleNode
将
元素选择为变量.