2010-07-30 33 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 
     { 
     } 

希望这有助于。