C# 简单的xml解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/552187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:50:45  来源:igfitidea点击:

simple xml parsing

c#.netxmlparsing

提问by ChrisCa

what is the simplest way to parse the lat and long out of the following xml fragment. There is no namespace etc.

从以下 xml 片段中解析经纬度的最简单方法是什么。没有命名空间等。

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 e.g. 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 中肯定有更简单的方法来做到这一点吗?

采纳答案by Mitch Wheat

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 SelectSingleNodeto select the <gpoint>element into a variable.

您当然可以使用 xpathSelectSingleNode<gpoint>元素选择为变量。

回答by Ash

Using Linq for XML:

将 Linq 用于 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(); 

回答by gix

If you can use LINQ for XML you can read those two values straight forward like this:

如果您可以使用 LINQ for 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 作为纬度。

回答by Robert Rossney

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);

回答by Mohamed Alikhan

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!

希望这可以帮助!