2017-07-17 37 views
0

我想解析GML並在Unity中使用數據。
這是解析XML我的C#代碼:如何在C#或JavaScript中解析GML以便與Unity一起使用

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using System.IO; 
using System.Xml.Linq; 
using System.Linq; 
using UnityEngine.UI; 

public class Manager_02 : MonoBehaviour 
{ 
    public GameObject text01; 

    private void Start() 
    { 
    string path = @"C:unitySample.gml"; 

    XDocument xd = XDocument.Load(path); 
    XNamespace gml = "http://www.opengis.net/gml"; 

    Text txt = GameObject.Find("Text").GetComponent<Text>(); 
    //this is for unity 

    var query = xd.Descendants(gml + "coord") 
     .Select(e => new 
     { 
      X = (decimal)e.Element(gml + "X"), 
      Y = (decimal)e.Element(gml + "Y") 
     }); 

    foreach (var c in query) 
    { 
     txt.text = c.ToString(); 
    } 
    } 
} 

這種運作良好,當應用於此XML:

<?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:LinearRing> 
    </gml:outerBoundaryIs> 
    </gml:Polygon> 
</schema> 

然而,當我使用下面的XML,這是行不通的。

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<IndoorFeatures xmlns="http://www.opengis.net/indoorgml/1.0/core" 
       xmlns:gml="http://www.opengis.net/gml/3.2" 
       xmlns:ns4="http://www.opengis.net/indoorgml/1.0/navigation" 
       xmlns:xlink="http://www.w3.org/1999/xlink" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       gml:id="IFs" xsi:schemaLocation="http://www.opengis.net/indoorgml/1.0/core http://schemas.opengis.net/indoorgml/1.0/indoorgmlcore.xsd"> 
    <gml:name>IFs</gml:name> 
    <gml:boundedBy> 
     <gml:Envelope srsDimension="3" srsName="EPSG::4326"> 
      <gml:lowerCorner>112.1168351477 48.8817891374 10.0</gml:lowerCorner> 
      <gml:upperCorner>116.7830482115 88.0511182109 20.0</gml:upperCorner> 
     </gml:Envelope> 
    </gml:boundedBy> 
    <primalSpaceFeatures> 
     <PrimalSpaceFeatures gml:id="PS2"> 
      <gml:name>PS2</gml:name> 
      <gml:boundedBy xsi:nil="true"/> 
      <cellSpaceMember> 
       <CellSpace gml:id="C45"> 
        <gml:description>Usage=Room</gml:description> 
        <gml:name>C45</gml:name> 
        <gml:boundedBy xsi:nil="true"/> 
        <Geometry3D> 
         <gml:Solid gml:id="SOLID1"> 
          <gml:exterior> 
           <gml:Shell> 
            <gml:surfaceMember> 
             <gml:Polygon gml:id="POLY56"> 
              <gml:name>POLY56</gml:name> 
              <gml:exterior> 
               <gml:LinearRing> 
                <gml:pos>114.7255054432 56.357827476 10.0</gml:pos> 
               </gml:LinearRing> 
              </gml:exterior> 
             </gml:Polygon> 
            </gml:surfaceMember> 
           </gml:Shell> 
          </gml:exterior> 
         </gml:Solid> 
        </Geometry3D> 
        <duality xlink:href="#R1"/> 
        <partialboundedBy xlink:href="#Door1"/> 
        <partialboundedBy xlink:href="#CB220"/> 
        <partialboundedBy xlink:href="#CB221"/> 
       </CellSpace> 
      </cellSpaceMember> 
     </PrimalSpaceFeatures> 
    </primalSpaceFeatures> 
</IndoorFeatures> 

我自然更喜歡使用簡單的代碼如下所示:

var query = xd.Descendants(gml + "LinearRing").Select(
    e => new 
    { 
     X = (decimal)e.Element(gml + "pos") 
    } 
); 

我如何才能使用統一解析GML在C#(或JavaScript)?

+0

我重新闡述了標題和問題,改進了一些語法和代碼格式。 – zx485

回答

0

嘗試這樣的代碼。你有名字空間問題:

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

namespace ConsoleApplication65 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      XElement indoorFeatures = doc.Root; 

      XNamespace xsGml= indoorFeatures.GetNamespaceOfPrefix("gml"); 
      XNamespace ns = indoorFeatures.GetDefaultNamespace(); 

      var results = doc.Elements(ns + "IndoorFeatures").Select(x => new { 
       name = (string)x.Element(xsGml + "name"), 
       lowerCorner = (string)x.Descendants(xsGml + "lowerCorner").FirstOrDefault(), 
       upperCorner = (string)x.Descendants(xsGml + "upperCorner").FirstOrDefault(), 
       primalSpaceFeatures = x.Descendants(ns + "PrimalSpaceFeatures").Select(y => new { 
        name = (string)y.Element(xsGml + "name"), 
        cellSpace = (string)y.Descendants(ns + "CellSpace").FirstOrDefault().Attribute(xsGml + "id"), 
        description = (string)y.Descendants(xsGml + "description").FirstOrDefault(), 
        cellSpaceName = (string)y.Descendants(ns + "CellSpace").FirstOrDefault().Element(xsGml + "name") 
       }).ToList() 
      }).FirstOrDefault(); 

     } 
    } 

} 
相關問題