How can I access a single XML element's value using C#.net web-pages with WebMatrix?

一曲冷凌霜 提交于 2019-12-03 15:24:53
@using System.Xml.Linq

XDocument doc = XDocument.Load(Server.MapPath("~/User_Saves/cradebaugh/testFile.xml"));

foreach (XElement element in doc.Element("root").Element("requisitionData").Descendants())
{
    string value = element.Value;
}

or with XPath:

@using System.Xml.Linq
@using System.Xml.XPath

XDocument doc = XDocument.Load(Server.MapPath("~/User_Saves/cradebaugh/testFile.xml"));

foreach (XElement element in doc.XPathSelectElement("//requisitionData").Descendants())
{
    string value = element.Value;
}

UPDATE:

And if you wanted to select for example the second <element1> node from your updated example:

string value = doc.XPathSelectElement("//requisitionData[2]/element1").Value;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!