我想解析GML并使用Unity中的数据 这是我用于解析XML的C#代码:
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")
}
);
如何解析C#(或JavaScript)中的GML才能使用unity?
答案 0 :(得分: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();
}
}
}