2010-07-30 32 views
2

我想通過循環來解析xml。我提到this,但我不能使用Load函數解析它,因爲它需要一個URI參數而不是一個字符串,LINQ to XML也是如此......任何人都可以幫我解決問題嗎?如何解析XML並從字符串中循環?

+0

請參閱[ 從字符串填充XDocument ](http://stackoverflow.com/questions/747554/populate-xdocument-from-string)。 – 2010-07-30 01:33:41

回答

0
string recentFileName = Path.Combine(folderPath, filexml); 
XDocument xDoc = XDocument.Load(recentFileName); 
+2

而不是使用XmlDocument,請使用XDocument。 xml-to-linq將會類似。 – 2010-07-30 01:26:37

0
 XmlDocument doc = new XmlDocument(); 
     doc.LoadXml("<root>" + 
        "<elem>some text<child/>more text</elem>" + 
        "</root>"); 
0

與XDocument類似,我們可以使用具有Parse方法的XElement來解析xmlString。

看到這個代碼:

 string xmlString = @"<poi><city>stockholm</city><country>sweden</country><gpoint><lat>51.1</lat><lng>67.98</lng></gpoint></poi>"; 
     try 
     { 
      XElement x = XElement.Parse(xmlString); 
      var latLng = x.Element("gpoint"); 

      Console.WriteLine(latLng.Element("lat").Value); 
      Console.WriteLine(latLng.Element("lng").Value); 
     } 
     catch 
     { 
     } 

希望這有助於。