2017-01-02 49 views
0

這是我的XML如何使用c#dom解析xml,但不使用linqToXml?

<Placemark> 
    <name>KOTAK ABC SCHOOL</name> 
    <address>KOTAK ABC SCHOOL,Pincode-00000</address> 
    <styleUrl>#icon-1899-0288D1</styleUrl> 
    <ExtendedData> 
     <Data name="School Address"> 
      <value>KOTAK ABC SCHOOL,</value> 
     </Data> 
     <Data name="unnamed (1)"> 
     </Data> 
     <Data name="coordinates"> 
     </Data> 
     <Data name="unnamed (2)"> 
     </Data> 
     <Data name="unnamed (3)"> 
     </Data> 
    </ExtendedData> 
    <MultiGeometry> 
     <Point> 
      <coordinates>83.334596,17.744104,0</coordinates> 
     </Point> 
     <LinearRing> 
      <coordinates> 
       83.32842825758384,17.71897819318788,0 83.32842825758384,17.72522980681212,0 83.33676374241615,17.72522980681212,0 83.33676374241615,17.71897819318788,0 83.32842825758384,17.71897819318788,0 
      </coordinates> 
     </LinearRing> 
    </MultiGeometry> 
</Placemark> 

我想要得到的名稱和標記座標內MultiGeometry \點

XmlDataDocument xmldoc = new XmlDataDocument(); 
XmlNodeList xmlnode; 
FileStream fs = new FileStream("C:\\Schools.xml", FileMode.Open, FileAccess.Read); 
xmldoc.Load(fs); 
xmlnode = xmldoc.GetElementsByTagName("Placemark"); 
foreach() ? 

我可以使用的foreach方法將內節點特拉弗斯?我只能使用C#DOM

回答

2
XmlDataDocument xmldoc = new XmlDataDocument(); 
FileStream fs = new FileStream("test.xml", FileMode.Open, FileAccess.Read); 
xmldoc.Load(fs); 
XmlNodeList _ngroups = xmldoc.GetElementsByTagName("Placemark"); 
foreach(XmlNode nd in _ngroups) 
{ 
Console.WriteLine(nd.ChildNodes[4].ChildNodes[0].InnerText.ToString()); 
Console.WriteLine(nd.ChildNodes[4].ChildNodes[1].InnerText.ToString()); 
} 

OR

XmlNodeList _ngroups = xmldoc.GetElementsByTagName("Placemark"); 
foreach(XmlNode nd in _ngroups) 
{ 
if(nd.ChildNodes[nd.ChildNodes.Count-1].Name=="MultiGeometry") 
{ 
Console.WriteLine(nd.ChildNodes[nd.ChildNodes.Count-1].ChildNodes[0].InnerText.ToString()); 
} 
    if(nd.ChildNodes[nd.ChildNodes.Count-1].ChildNodes[1].Name=="LinearRing") 
{ 
Console.WriteLine(nd.ChildNodes[nd.ChildNodes.Count-1].ChildNodes[1].InnerText.ToString()); 
} 
+0

給system.NullRefrenceException –

+0

問題解決。我更新了答案中的代碼。 –

+0

這種異常仍然存在! –