对于带有命名空间的XML,XMLNodeList始终为空

时间:2012-12-07 19:02:25

标签: c# xml c#-4.0 xml-parsing

我有以下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命名空间有关。有点帮助指出我正确的方向将是伟大的。

1 个答案:

答案 0 :(得分:2)

使用前缀为kml的XPATH标识命名空间管理器中的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
}