1

在此 XML 中导航时苦苦挣扎。我似乎无法定义正确的 xPath。我是 XML 新手,可以通过在子节点中移动来导航,但不能直接导航到节点。这是我用来尝试读取 XML 的代码:

fs As FileStream = New FileStream(xmlFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
xmlDoc.Load(fs)
Dim nodeName As String = "//map/Layer"
'Try and navigate to the Map Layer.
Dim tnodeToRetrieve = xmlDoc.DocumentElement.SelectNodes(nodeName)

这是我正在尝试阅读的 XML:

<map:Map xmlns="java:com.raytheon.atc.chi.graphics.geom2d" 
 xmlns:map="java:com.raytheon.atc.chi.adt.map" 
 unit="m" lat="41.975953650" lon="-87.903248430" alt="78.33">
    <!--ASDE-X DP map for facility:ord Created 10/20/2009 2:22:31 PM-->
    <map:Layer isVisible="true">
        <map:Line type="Runway"></map:Line>
        <map:Region type="Runway">
            <Surface operation="union">
                <Polygon>
                    <Point x="1618.87" y="-742.10"></Point>
                    <Point x="1617.71" y="-742.11"></Point>
                    <Point x="1459.74" y="-742.52"></Point>
                    <Point x="1058.11" y="-743.35"></Point>
                    <Point x="1058.11" y="-743.35"></Point>
                    <Point x="996.84" y="-743.28"></Point>
                    <Point x="752.91" y="-744.21"></Point>
                    <Point x="689.86" y="-744.13"></Point>
                    <Point x="584.80" y="-744.42"></Point>
                    <Point x="484.89" y="-744.59"></Point>
                    <Point x="443.15" y="-744.69"></Point>
                    <Point x="331.33" y="-744.92"></Point>
                    <Point x="200.05" y="-745.00"></Point>
                    <Point x="122.49" y="-745.09"></Point>
                    <Point x="-10.78" y="-745.60"></Point>
                    <Point x="-311.43" y="-745.98"></Point>
                    <Point x="-459.40" y="-746.47"></Point>
                    <Point x="-421.28" y="-791.45"></Point>
                    <Point x="-239.66" y="-791.03"></Point>
                    <Point x="-149.83" y="-791.02"></Point>
                    <Point x="128.25" y="-790.53"></Point>
                    <Point x="192.65" y="-790.23"></Point>
                    <Point x="484.35" y="-789.69"></Point>
                    <Point x="506.50" y="-789.65"></Point>
                    <Point x="526.45" y="-789.65"></Point>
                    <Point x="546.57" y="-789.56"></Point>
                    <Point x="689.71" y="-789.30"></Point>
                    <Point x="759.52" y="-789.17"></Point>
                    <Point x="993.69" y="-788.87"></Point>
                    <Point x="1063.68" y="-788.69"></Point>
                    <Point x="1459.89" y="-787.87"></Point>
                    <Point x="1545.93" y="-787.66"></Point>
                    <Point x="1618.73" y="-787.11"></Point>
                </Polygon>
                <Polygon>
                    <Point x="589.58" y="2384.47"></Point>
                    <Point x="554.74" y="2412.44"></Point>
                    <Point x="382.81" y="2204.74"></Point>
                    <Point x="365.25" y="2183.32"></Point>
                    <Point x="260.02" y="2055.88"></Point>
                    <Point x="249.17" y="2042.69"></Point>
                    <Point x="224.60" y="2012.83"></Point>
                    <Point x="211.32" y="1996.69"></Point>
                    <Point x="16.82" y="1760.35"></Point>
                    <Point x="46.31" y="1725.16"></Point>
                    <Point x="214.95" y="1929.82"></Point>
                    <Point x="258.69" y="1982.84"></Point>
                    <Point x="388.78" y="2140.72"></Point>
                    <Point x="413.20" y="2170.36"></Point>
                    <Point x="555.91" y="2343.46"></Point>
                    <Point x="587.13" y="2381.17"></Point>
                    <Point x="589.58" y="2384.47"></Point>
                </Polygon>
                <Polygon>
                    <Point x="16.82" y="1760.35"></Point>
                    <Point x="-12.71" y="1724.78"></Point>
                    <Point x="17.03" y="1689.53"></Point>
                    <Point x="46.31" y="1725.16"></Point>
                    <Point x="16.82" y="1760.35"></Point>
                </Polygon>
                <Polygon>
                    <Point x="16.82" y="1760.35"></Point>
                    <Point x="-425.19" y="2287.55"></Point>
                    <Point x="-461.48" y="2330.71"></Point>
                    <Point x="-588.61" y="2481.95"></Point>
                    <Point x="-599.14" y="2472.97"></Point>
                    <Point x="-612.67" y="2461.55"></Point>
                    <Point x="-623.31" y="2452.62"></Point>
                    <Point x="-515.90" y="2324.65"></Point>
                    <Point x="-467.85" y="2267.21"></Point>
                    <Point x="-406.86" y="2194.77"></Point>
                    <Point x="-331.08" y="2104.45"></Point>
                    <Point x="-237.56" y="1993.12"></Point>
                    <Point x="-122.79" y="1856.07"></Point>
                    <Point x="-12.71" y="1724.78"></Point>
                    <Point x="16.82" y="1760.35"></Point>
                </Polygon>
            </Surface>
        </map:Region>
    </map:Layer>
</map:Map>
4

2 回答 2

2

实际上有不止一个命名空间。因此,虽然添加"java:com.raytheon.atc.chi.adt.map"命名空间将允许您选择图层和区域,但为了同时选择多边形和点,您需要为两者添加命名空间解析器。这是一个例子:

Imports System
Imports System.Xml
Imports System.Xml.XPath
Imports System.Linq
Imports System.Xml.Linq

Module Module1

    Dim s As XDocument =
<?xml version="1.0" encoding="UTF-8"?>
<map:Map xmlns="java:com.raytheon.atc.chi.graphics.geom2d" xmlns:map="java:com.raytheon.atc.chi.adt.map" unit="m" lat="41.975953650" lon="-87.903248430" alt="78.33"><!--ASDE-X DP map for facility:ord Created 10/20/2009 2:22:31 PM-->
    <map:Layer isVisible="true"><map:Line type="Runway"></map:Line>
        <map:Region type="Runway"><Surface operation="union">
            <Polygon><Point x="1618.87" y="-742.10"></Point>
                <Point x="1459.74" y="-742.52"></Point>
                <Point x="1058.11" y="-743.35"></Point>
            </Polygon>
            <Polygon><Point x="16.82" y="1760.35"></Point>
                <Point x="46.31" y="1725.16"></Point>
                <Point x="16.82" y="1760.35"></Point>
            </Polygon>
            <Polygon><Point x="16.82" y="1760.35"></Point>
                <Point x="16.82" y="1760.35"></Point>
            </Polygon>
            </Surface>
        </map:Region>
    </map:Layer>
</map:Map>
    Sub Main()

        Dim ns As New XmlNamespaceManager(New NameTable())

        ns.AddNamespace("x", "java:com.raytheon.atc.chi.graphics.geom2d")
        ns.AddNamespace("m", "java:com.raytheon.atc.chi.adt.map")

        Console.WriteLine("Layers:")
        Dim elements = s.XPathSelectElements("//m:*//m:Layer", ns)
        For Each element As XElement In elements
            Console.WriteLine(element.Name)
        Next
        Console.WriteLine("Polygons:")
        elements = s.XPathSelectElements("//m:*//m:Region/x:Surface/x:Polygon", ns)
        For Each element As XElement In elements
            Console.WriteLine(element.Name)
        Next
        Console.WriteLine("Points:")
        elements = s.XPathSelectElements("//m:*//m:Region/x:Surface/x:Polygon/x:Point", ns)
        For Each element As XElement In elements
            Console.WriteLine(element.Name)
        Next
        Console.ReadKey()



    End Sub

End Module

这将输出:

Layers:
{java:com.raytheon.atc.chi.adt.map}Layer
Polygons:
{java:com.raytheon.atc.chi.graphics.geom2d}Polygon
{java:com.raytheon.atc.chi.graphics.geom2d}Polygon
{java:com.raytheon.atc.chi.graphics.geom2d}Polygon
Points:
{java:com.raytheon.atc.chi.graphics.geom2d}Point
{java:com.raytheon.atc.chi.graphics.geom2d}Point
{java:com.raytheon.atc.chi.graphics.geom2d}Point
{java:com.raytheon.atc.chi.graphics.geom2d}Point
{java:com.raytheon.atc.chi.graphics.geom2d}Point
{java:com.raytheon.atc.chi.graphics.geom2d}Point
{java:com.raytheon.atc.chi.graphics.geom2d}Point
{java:com.raytheon.atc.chi.graphics.geom2d}Point

或者,您可以选择使用 [local-name()="Name-Without-Namspace"]。但上面更干净。

于 2012-12-15T13:52:10.343 回答
1

您需要指定命名空间。没有Map元素,只有一个map:Map元素。要定义命名空间,您必须创建一个命名空间管理器,如下所示:

fs As FileStream = New FileStream(xmlFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
xmlDoc.Load(fs)
Dim nm As New XmlNamespaceManager(xmlDoc.NameTable)
nm.AddNamespace("map", "java:com.raytheon.atc.chi.adt.map")
Dim nodeName As String = "//map:Map/map:Layer"
Dim tnodeToRetrieve = xmlDoc.DocumentElement.SelectNodes(nodeName, nm)
于 2012-12-15T13:03:20.653 回答