gpt4 book ai didi

c# - 简单的xml解析

转载 作者:数据小太阳 更新时间:2023-10-29 01:45:41 25 4
gpt4 key购买 nike

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

它在一个字符串变量中。不是流。

<poi>
<city>stockholm</city>
<country>sweden</country>
<gpoint>
<lat>51.1</lat>
<lng>67.98</lng>
</gpoint>
</poi>

到目前为止,我读到的所有内容对于本应是一项简单的任务来说都太复杂了例如 Link

我一直在看上面的链接

在 .net 中肯定有更简单的方法来做到这一点吗?

最佳答案

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

关于c# - 简单的xml解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/552187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com