2012-12-07 14 views
1

一個XML我有以下XML文件:XmlNodeList中總是空的名稱空間爲

<?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命名空間有關。幫助我指出正確的方向會很好。

+0

任何理由使用該'old'的XML命名空間的KML API而不是'linq2xml' – Anirudha

+0

沒有理由。只是我更舒服的一點,我剛開始使用.NET,並沒有多少探索LINQ。 –

+0

結帳my ans for linq2xml解決方案 – Anirudha

回答

2

使用XPATH前綴KML標識在您的命名空間管理

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

OR

使用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 
} 
+0

好的我應該知道,但即使在函數調用中放入'XmlNamespaceManager'後,我在XMLNodeList中獲得了0個元素。是的,我檢查了我正在閱讀正確的文件。 –

+0

我應該提到,我曾經嘗試過,沒有任何效果。 XMLNodeList中仍然有0個元素 –

+0

工作正常,但是我的原始解決方案有什麼問題? –

相關問題