[英]How to parse xml and loop through it from a string?
I want parse xml by looping through it. 我想通过循环解析xml。 I referred this but I am not able to parse it using the Load function since it expects an URI parameter and not a string and so does LINQ to XML....Can anyone help me out ? 我提到了这个问题,但是我无法使用Load函数来解析它,因为它需要一个URI参数而不是一个字符串,因此LINQ to XML也可以。...有人可以帮助我吗?
XmlDocument
has a Load
method which takes a filename, but also a LoadXml
method which takes a string: XmlDocument
有一个采用文件名的Load
方法,还有一个采用字符串的LoadXml
方法:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx
Similarly, XDocument
has a Load
method which takes a filename, or a Parse
method which takes a string: 同样, XDocument
具有一个使用文件名的Load
方法或一个使用字符串的Parse
方法:
http://msdn.microsoft.com/en-us/library/bb345532.aspx http://msdn.microsoft.com/en-us/library/bb345532.aspx
Similar to XDocument, we can use XElement which has Parse method to parse a xmlString. 与XDocument相似,我们可以使用具有Parse方法的XElement来解析xmlString。
See this code: 参见以下代码:
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
{
}
Hope this helps. 希望这可以帮助。
string recentFileName = Path.Combine(folderPath, filexml);
XDocument xDoc = XDocument.Load(recentFileName);
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>" +
"<elem>some text<child/>more text</elem>" +
"</root>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.