在C#中解析gml文件时,如何选择具有特定属性的特定元素?

时间:2018-10-26 16:40:57

标签: c# linq parsing

我正在尝试在C#中解析gml文件。这就是为什么我需要选择一些特定的元素。我想做的是选择位于<lifr:LineString gml:id="ls1">

中的坐标

我的gml文件的一部分如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:gml ="http://www.opengis.net/gml/3.2" xmlns:lifr="http://www.opengis.net/infragml/road/1.0" xmlns:xlink="http://www.opengis.net/infragml/road/1.0">
 <lifr:stringLineSet>
  <lifr:StringLineSet gml:id="sls1">
    <gml:description>string lines delineating the top pavement surface</gml:description>
    <gml:name>top surface pavement</gml:name>
    <lifr:stringLineSetID>
      <lifr:ID>
        <identifier>pavement1</identifier>
      </lifr:ID>
    </lifr:stringLineSetID>
    <lifr:stringLine>
      <lifr:StringLine gml:id="sl1">
        <gml:description>top surface</gml:description>
        <gml:name>left edge pavement</gml:name>
        <lifr:stringLineID>
          <lifr:ID>
            <identifier>LEP</identifier>
          </lifr:ID>
        </lifr:stringLineID>
        <lifr:geometry>
          <lifr:LineString gml:id="ls1">
            <gml:pos>-3.650 1000 49.927</gml:pos>
            <gml:pos>-3.650 1100 48.927</gml:pos>
          </lifr:LineString>
        </lifr:geometry>
      </lifr:StringLine>
    </lifr:stringLine>
    <lifr:stringLine>
      <lifr:StringLine gml:id="sl2">
        <gml:description>top surface</gml:description>
        <gml:name>centerline pavement</gml:name>
        <lifr:stringLineID>
          <lifr:ID>
            <identifier>CLP</identifier>
          </lifr:ID>
        </lifr:stringLineID>
        <lifr:geometry>
          <lifr:LineString gml:id="ls2">
            <gml:pos>0.000 1000 50.000</gml:pos>
            <gml:pos>0.000 1100 49.000</gml:pos>
          </lifr:LineString>
        </lifr:geometry>
        <lifr:alternativeGeometry xlink:href="ac1"/>
      </lifr:StringLine>
    </lifr:stringLine>
    <lifr:stringLine>
      <lifr:StringLine gml:id="sl3">
        <gml:description>top surface</gml:description>
        <gml:name>right edge pavement</gml:name>
        <lifr:stringLineID>
          <lifr:ID>
            <identifier>REP</identifier>
          </lifr:ID>
        </lifr:stringLineID>
        <lifr:geometry>
          <lifr:LineString gml:id="ls3">
            <gml:pos>3.650 1000 49.927</gml:pos>
            <gml:pos>3.650 1100 48.927</gml:pos>
          </lifr:LineString>
        </lifr:geometry>
      </lifr:StringLine>
    </lifr:stringLine>
  </lifr:StringLineSet>
</lifr:stringLineSet>
</root>

我试图用下面的代码编写坐标到控制台,但是我只是空白控制台。

        XNamespace gml = "http://www.opengis.net/gml/3.2";
        XNamespace lifr = "http://www.opengis.net/infragml/road/1.0";
        XDocument xmlDoc = XDocument.Load(@"C:\Road_example.gml"); 

        IEnumerable<XElement> pavement = 
            from el in xmlDoc.Elements().Elements(lifr + "LineString")
            where (string)el.Attribute(gml + "id") == "ls1"
            select xmlDoc.Parent.Element(gml + "pos");
        foreach (XElement coords in pavement)

        Console.WriteLine(coords);
        Console.ReadKey();

1 个答案:

答案 0 :(得分:0)

这是解决方法:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication75
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {

            XNamespace gml = "http://www.opengis.net/gml/3.2";
            XNamespace lifr = "http://www.opengis.net/infragml/road/1.0";
            XDocument xmlDoc = XDocument.Load(FILENAME);

            List<XElement> pavement = xmlDoc.Descendants(lifr + "LineString").ToList();

            foreach (XElement coords in pavement)
            {
                string id = (string)coords.Attribute(gml + "id");
                string pos = string.Join(",", coords.Elements(gml + "pos").Select(x => (string)x));

                Console.WriteLine("id = '{0}', positions = '{1}'", id, pos);

            }

            //with filtered results
            var ls1 = pavement.Where(x => (string)x.Attribute(gml + "id") == "ls1").FirstOrDefault();
            string positions = string.Join(",", ls1.Elements(gml + "pos").Select(x => (string)x));


            Console.ReadKey();
        }

    }

}
相关问题