[英]How to parse xml and loop through it from a string?
XmlDocument
有一個采用文件名的Load
方法,還有一個采用字符串的LoadXml
方法:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml.aspx
同樣, XDocument
具有一個使用文件名的Load
方法或一個使用字符串的Parse
方法:
與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
{
}
希望這可以幫助。
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.