如何使用带有 WebMatrix 的 C#.net 网页访问单个 XML 元素的值? [英] How can I access a single XML element's value using C#.net web-pages with WebMatrix?
问题描述
我查看了大量资源,进行了大量研究,并尝试了许多最佳猜测"来使用 WebMatrix 和 C#、网页一次访问单个元素,但是我没有尝试的是通过.
I've looked at a lot of resources, done a lot of research, and tried many "best-guesses" to access a single element at a time using WebMatrix with C#, web-pages, however nothing I am trying is getting through.
考虑一个如下所示的简单 xml 文档:
Consider a simple xml document that looks like this:
<root>
<requisitionData>
<element1>I am element 1</element1>
<element2>I am element 2</element2>
</requisitionData>
</root>
我知道我可以使用 foreach 循环,如下所示:
I know I can use a foreach loop, like so:
@using System.Xml.Linq
XDocument doc = XDocument.Load(Server.MapPath("~/User_Saves/cradebaugh/testFile.xml"));
foreach (XElement element in doc.Descendants("requisitionData"))
{
@element.Value
}
当然,这很好用.但是,如果我只是想将单个元素、
的值存储在字符串变量中呢?
And that, of course, works fine. But what if I simply wanted to store the single element, <element1>
's value in a string variable?
我看过这里(下面的链接),但我无法理解这段代码的正面或反面(对我来说,它甚至看起来都不像 C#,但话说回来,我对解析 XML 还是很陌生...):
I've looked here (link below), but I can't make heads or tails of this code (it barely even looks like C# to me, but then again, I'm so new to parsing XML...):
我也在这里检查过:如何从 XDocument 获取 XML 节点
但是这里显示的代码对我来说也没有意义.我一直认为必须有一种更简单的方法来做到这一点,希望无需学习全新的查询方法.
But the code shown makes no sense to me here either. I keep thinking there must be a simpler way to do this, hopefully without learning a whole new querying approach.
---------------------------------我尝试过的东西----------------------------------
---------------------------------THINGS I'VE TRIED---------------------------------
XDocument doc = XDocument.Load(Server.MapPath("~/User_Saves/cradebaugh/testFile.xml"));
string element = doc.Descendants("requisitionData").Descendants("element1").Value;
我收到的错误:缺少使用指令或程序集引用
Error I receive: "missing using directive or assembly reference
XDocument doc = XDocument.Load(Server.MapPath("~/User_Saves/cradebaugh/testFile.xml"));
XElement element = doc.Descendants("element1");
string val = element.Value;
我收到的错误: 无法将类型System.Collections.Generic.IEnumerable"隐式转换为System.Xml.Linq.XElement".存在显式转换(您是否缺少强制转换?)
Error I receive: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Xml.Linq.XElement'. An explicit conversion exists (are you missing a cast?)
我确实尝试过其他方法,但我遇到了与上面显示的几乎相同的错误.是我让这件事变得更难了,还是我过于简单化了?
I have, indeed, tried other things, but I get pretty much the same errors as shown above. Am I making this harder than it is, or am I oversimplifying it?
-------------------------更新------------------------------
-------------------------UPDATE------------------------------
我能够让它发挥作用:
string element = doc.Element("root").Element("requisitionData").Element("element1").Value;
@element
但是,关于这种方法,我担心的一件事是 .Element
选择第一个"匹配项,因此在如下所示的 xml 文档中:
However, one thing that concerns me about this approach is that .Element
selects the 'first' match, so in an xml document that looks like this:
<root>
<requisitionData>
<element1>I am element 1</element1>
<element2>I am element 2</element2>
</requisitionData>
<requisitionData>
<element1>I am element 1</element1>
<element2>I am element 2</element2>
</requisitionData>
</root>
如何访问第二次出现的
?
How could I access the second occurrence of <element1>
?
推荐答案
@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;
}
或使用 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;
}
<小时>
更新:
如果您想从更新的示例中选择例如第二个
节点:
And if you wanted to select for example the second <element1>
node from your updated example:
string value = doc.XPathSelectElement("//requisitionData[2]/element1").Value;
这篇关于如何使用带有 WebMatrix 的 C#.net 网页访问单个 XML 元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!