C# 如何从XML节点提取值?

C# 如何从XML节点提取值?,c#,xml,C#,Xml,我有一个字符串,如下所示: xmlString=@"<product><name>abc</name><price>9.8</price></product>"; 如果给定xmlString,我怎么做?试试这个 var product = XElement.Parse(xmlString); var price = (decimal)product.Element("price"); string xmlString =

我有一个字符串,如下所示:

xmlString=@"<product><name>abc</name><price>9.8</price></product>";
如果给定xmlString,我怎么做?

试试这个

var product = XElement.Parse(xmlString);
var price = (decimal)product.Element("price");
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);
  }
string xmlString=@“abc9.8”;
XmlDataDocument xmlDoc=新的XmlDataDocument();
LoadXml(xmlString);
XmlNodeList=xmlDoc.SelectNodes(“产品/价格”);
foreach(列表中的XmlNode)
{
Console.WriteLine(n.ChildNodes[0].Value);
}

XDocument.Parse(xmlString.Element(“price”).value我希望你等到他花了点力气才给他答案。看看他的其他问题——他已经知道LINQ到XML。
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);
  }