[英]simple xml parsing
what is the simplest way to parse the lat and long out of the following xml fragment. 从以下xml片段中解析经纬度的最简单方法是什么? There is no namespace etc. 没有名称空间等。
It is in a string variable. 它在字符串变量中。 not a stream. 不是流。
<poi>
<city>stockholm</city>
<country>sweden</country>
<gpoint>
<lat>51.1</lat>
<lng>67.98</lng>
</gpoint>
</poi>
everything I have read so far is waaaaay too complex for what should be a simple task eg http://geekswithblogs.net/kobush/archive/2006/04/20/75717.aspx 到目前为止,我读过的所有内容对于一个简单的任务来说都是太复杂了,例如http://geekswithblogs.net/kobush/archive/2006/04/20/75717.aspx
I've been looking at the above link 我一直在看上面的链接
Surely there is a simpler way to do this in .net? 当然,.net中有一种更简单的方法吗?
Using Linq for XML: 使用Linq for XML:
XDocument doc= XDocument.Parse("<poi><city>stockholm</city><country>sweden</country><gpoint><lat>51.1</lat><lng>67.98</lng></gpoint></poi>");
var points=doc.Descendants("gpoint");
foreach (XElement current in points)
{
Console.WriteLine(current.Element("lat").Value);
Console.WriteLine(current.Element("lng").Value);
}
Console.ReadKey();
using System.IO;
using System.Xml;
using System.Xml.XPath;
. 。 . 。 . 。
string xml = @"<poi>
<city>stockholm</city>
<country>sweden</countr>
<gpoint>
<lat>51.1</lat>
<lng>67.98</lng>
</gpoint>
</poi>";
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"));
You could of course use xpath SelectSingleNode
to select the <gpoint>
element into a variable. 您当然可以使用xpath SelectSingleNode
将<gpoint>
元素选择为变量。
Even simpler than Mitch Wheat's answer, since the fragment in question is a well-formed XML document: 甚至比Mitch Wheat的答案更简单,因为所讨论的片段是格式正确的XML文档:
using System.Xml;
using System.IO;
...
string xml = @"<poi>
<city>stockholm</city>
<country>sweden</country>
<gpoint>
<lat>51.1</lat>
<lng>67.98</lng>
</gpoint>
</poi>";
XmlDocument d = new XmlDocument();
d.Load(new StringReader(xml));
Console.WriteLine(d.SelectSingleNode("/poi/gpoint/lat").InnerText);
Console.WriteLine(d.SelectSingleNode("/poi/gpoint/lng").InnerText);
If you can use LINQ for XML you can read those two values straight forward like this: 如果可以将LINQ用于XML,则可以像这样直接读取这两个值:
var doc = XDocument.Parse("...");
var point = doc.Element("poi").Element("gpoint");
Console.WriteLine("Lat: {0}, Lng: {1}",
point.Element("lat").Value,
point.Element("lng").Value);
If you need them as double you have to convert them: 如果您需要将它们加倍,则必须对其进行转换:
double lat = Convert.ToDouble(point.Element("lat").Value,
CultureInfo.InvariantCulture);
Don't forget to specify a culture that uses a dot for fractions. 不要忘记指定使用点表示分数的区域性。 Otherwise this would yield 511.0 as latitude. 否则,这将产生511.0的纬度。
Use XElement.Parse(string text) method to do this. 使用XElement.Parse(string text)方法执行此操作。
Example: 例:
string xmlString = @"<poi>
<city>stockholm</city>
<country>sweden</country>
<gpoint>
<lat>51.1</lat>
<lng>67.98</lng>
</gpoint>
</poi>";
try
{
XElement x = XElement.Parse(xmlString);
var latLng = x.Element("gpoint");
Console.WriteLine(latLng.Element("lat").Value);
Console.WriteLine(latLng.Element("lng").Value);
}
catch
{
}
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.