1

我有以下 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    <Document>
        <name>STO.kmz</name>
        <open>1</open>
        <StyleMap id="msn_A26">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_A100</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_A00</styleUrl>
            </Pair>
        </StyleMap>
        <Style id="sn_square18">
            <IconStyle>
                <scale>1.2</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="sn_ylw-diamond1">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/ylw-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/ylw-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sh_A">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/A.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/A-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <StyleMap id="msn_blu-diamond8">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_blu-diamond00</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_blu-diamond8</styleUrl>
            </Pair>
        </StyleMap>
        <Style id="sh_square10">
            <IconStyle>
                <scale>1.4</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <StyleMap id="msn_ylw-diamond5">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_ylw-diamond0</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_ylw-diamond5</styleUrl>
            </Pair>
        </StyleMap>
        <Style id="sh_blu-diamond13">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sn_blu-diamond31">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sh_square7">
            <IconStyle>
                <scale>1.4</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="sh_square19">
            <IconStyle>
                <scale>1.4</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="sn_blu-diamond8">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sn_square16">
            <IconStyle>
                <scale>1.2</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="sn_blu-diamond1">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sh_square8">
            <IconStyle>
                <scale>1.4</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="s_ylw-pushpin_hl00">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
                </Icon>
                <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <LineStyle>
                <color>ff7f55aa</color>
                <width>2.9</width>
            </LineStyle>
        </Style>
        <Style id="sn_blu-diamond01">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sn_blu-diamond60">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <StyleMap id="msn_blu-diamond3">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_blu-diamond</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_blu-diamond7</styleUrl>
            </Pair>
        </StyleMap>
        <StyleMap id="msn_square31">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_square47</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_square28</styleUrl>
            </Pair>
        </StyleMap>
        <Style id="sh_blu-diamond9">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sn_square100">
            <IconStyle>
                <scale>1.2</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <StyleMap id="msn_A28">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_A33</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_A150</styleUrl>
            </Pair>
        </StyleMap>
        <Style id="sh_A8">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/A.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/A-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <StyleMap id="msn_square27">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_square35</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_square11</styleUrl>
            </Pair>
        </StyleMap>
        <Style id="sh_square20">
            <IconStyle>
                <scale>1.4</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="sh_ylw-diamond">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/ylw-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/ylw-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <StyleMap id="msn_square25">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_square240</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_square13</styleUrl>
            </Pair>
        </StyleMap>
        <Style id="sh_ylw-diamond5">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/ylw-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/ylw-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sh_blu-diamond">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sn_square37">
            <IconStyle>
                <scale>1.2</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="sn_A18">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/A.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/A-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sn_square130">
            <IconStyle>
                <scale>1.2</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="sn_square90">
            <IconStyle>
                <scale>1.2</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href>
                </Icon>
            </IconStyle>
        </Style>
        <Style id="sn_blu-diamond0">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <StyleMap id="msn_red-diamond">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_red-diamond</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_red-diamond</styleUrl>
            </Pair>
        </StyleMap>
        <Style id="sn_blu-diamond7">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sn_blu-diamond13">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sn_A19">
            <IconStyle>
                <scale>1.1</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/A.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/A-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <Style id="sh_blu-diamond31">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond.png</href>
                </Icon>
                <hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/>
            </IconStyle>
            <ListStyle>
                <ItemIcon>
                    <href>http://maps.google.com/mapfiles/kml/paddle/blu-diamond-lv.png</href>
                </ItemIcon>
            </ListStyle>
        </Style>
        <StyleMap id="msn_blu-diamond">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_blu-diamond0</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_blu-diamond</styleUrl>
            </Pair>
        </StyleMap>
        <StyleMap id="msn_blu-diamond11">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_blu-diamond3</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_blu-diamond50</styleUrl>
            </Pair>
        </StyleMap>
        <StyleMap id="msn_ylw-diamond2">
            <Pair>
                <key>normal</key>
                <styleUrl>#sn_ylw-diamond4</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#sh_ylw-diamond10</styleUrl>
            </Pair>
        </StyleMap>
        <Folder>
            <name>STO</name>
            <open>1</open>
            <Placemark>
                <name>2255</name>
                <LookAt>
                    <longitude>-75.79382956897737</longitude>
                    <latitude>45.43539940356241</latitude>
                    <altitude>0</altitude>
                    <heading>-3.906129488807351</heading>
                    <tilt>13.06994671404837</tilt>
                    <range>227.4175908854991</range>
                    <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
                </LookAt>
                <styleUrl>#msn_ylw-diamond</styleUrl>
                <Point>
                    <coordinates>-75.79406138001882,45.43526902479236,0</coordinates>
                </Point>
            </Placemark>
            <Placemark>
                <name>2253</name>
                <LookAt>
                    <longitude>-75.79051291553527</longitude>
                    <latitude>45.43588490633884</latitude>
                    <altitude>0</altitude>
                    <heading>-3.880874298803265</heading>
                    <tilt>18.63262195622465</tilt>
                    <range>167.8701172310251</range>
                    <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
                </LookAt>
                <styleUrl>#msn_ylw-diamond</styleUrl>
                <Point>
                    <coordinates>-75.7906693408864,45.43559495897022,0</coordinates>
                </Point>
            </Placemark>

    </Document>
    </kml>

我正在尝试Placemark使用我编写的以下代码访问标签中的所有节点:-

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(fileName);

        XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
        manager.AddNamespace("kml", "http://www.opengis.net/kml/2.2");
        manager.AddNamespace("gx", "http://www.google.com/kml/ext/2.2");
        manager.AddNamespace("atom", "http://www.w3.org/2005/Atom");

        XmlElement root = xmlDoc.DocumentElement;

        XmlNodeList dataNodes = xmlDoc.SelectNodes("Placemark");
        foreach (XmlNode node in dataNodes)
        {
            foreach (XmlNode childNode in node.ChildNodes)
            {
                 // My operations here    
            }
        }

现在的问题是 XMLNodeList 总是空的。我认为问题与 XML 命名空间有关。一点帮助指出我正确的方向会很棒。

4

1 回答 1

2

使用XPATH前缀 kml 来标识命名空间管理器中的 kml 命名空间

root.SelectNodes("//kml:Placemark",manager);
                  -----

或者

采用LINQ2xml

XElement doc=XElement.Load(yourXML);

XNamespace ns="http://www.opengis.net/kml/2.2";

foreach(XElement elm in doc.Descendants().Elements(ns+"Placemark"))
{
    elm.Element(ns+"name").Value;//your Placemark's name element value
    //remember to use ns foreach element and attributes
}
于 2012-12-07T19:06:08.593 回答