2012-02-15 150 views
0

我有一個字符串,如下所示:如何從XML節點中提取值?

[email protected]"<product><name>abc</name><price>9.8</price></product>"; 

我想提取價格的值,並將其賦值給一個變量Price

double Price = ??? 

我怎麼這麼給出的xmlString做?

+2

XDocument.Parse(的xmlString).Element( 「價格」)。價值 – IamStalker 2012-02-15 05:42:35

回答

6
var product = XElement.Parse(xmlString); 
var price = (decimal)product.Element("price"); 
+2

我希望你能等到他遞給他的答案之前花了一些功夫。看看他的其他問題 - 他已經知道LINQ to XML。 – 2012-02-15 05:45:00

3

試試這個

string xmlString = @"<product><name>abc</name><price>9.8</price></product>"; 
XmlDataDocument xmlDoc = new XmlDataDocument(); 
xmlDoc.LoadXml(xmlString); 

XmlNodeList list = xmlDoc.SelectNodes("product/price"); 

foreach (XmlNode n in list) 
    { 
    Console.WriteLine(n.ChildNodes[0].Value); 
    }