[英]Parsing GML data using C# Linq to XML
I know this is most likly very basic and been asked a thousand times but for some reason I just can't get it to work. 我知道这是非常基本的,并被问了一千次,但由于某种原因,我无法让它工作。
I have a gml file that looks like the following: 我有一个类似于以下内容的gml文件:
<?xml version='1.0' encoding='UTF-8'?>
<schema
xmlns='http://www.w3.org/2000/10/XMLSchema'
xmlns:gml='http://www.opengis.net/gml'
xmlns:xlink='http://www.w3.org/1999/xlink'
xmlns:xsi='http://www.w3.org/2000/10/XMLSchema-instance'
xsi:schemaLocation='http://www.opengis.net/gml/feature.xsd'>
<gml:Polygon srsName='http://www.opengis.net/gml/srs/epsg.xml#4283'>
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coord>
<gml:X>152.035953</gml:X>
<gml:Y>-28.2103190007845</gml:Y>
</gml:coord>
<gml:coord>
<gml:X>152.035957</gml:X>
<gml:Y>-28.2102020007845</gml:Y>
</gml:coord>
<gml:coord>
<gml:X>152.034636</gml:X>
<gml:Y>-28.2100120007845</gml:Y>
</gml:coord>
<gml:coord>
<gml:X>152.034617</gml:X>
<gml:Y>-28.2101390007845</gml:Y>
</gml:coord>
<gml:coord>
<gml:X>152.035953</gml:X>
<gml:Y>-28.2103190007845</gml:Y>
</gml:coord>
</gml:LinearRing>
</gml:outerBoundaryIs>
</gml:Polygon>
</schema>
All I need to be able to do is read the X and Y from each gml:coord node. 我需要做的就是从每个gml:coord节点读取X和Y. I am using C# 3.0 and LINQ so it should be easy but everything I try just returns empty results. 我正在使用C#3.0和LINQ,所以它应该很简单,但我尝试的只是返回空结果。
I have only done xml parsing in VB so the C# way is a bit foreign to me at the moment. 我只在VB中进行了xml解析,所以C#方式对我来说有点陌生。
Thanks, Nathan 谢谢,内森
My guess is that you haven't included the namespace. 我的猜测是你没有包含命名空间。 Here's a short but complete program which shows all the coords: 这是一个简短但完整的程序,显示所有的坐标:
using System;
using System.Linq;
using System.Xml.Linq;
class Test
{
static void Main()
{
XDocument doc = XDocument.Load("test.xml");
XNamespace gml = "http://www.opengis.net/gml";
var query = doc.Descendants(gml + "coord")
.Select(e => new { X = (decimal) e.Element(gml + "X"),
Y = (decimal) e.Element(gml + "Y") });
foreach (var c in query)
{
Console.WriteLine(c);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.