如何使用带有 WebMatrix 的 C#.net 网页访问单个 XML 元素的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15903671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:23:46  来源:igfitidea点击:

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

c#xmlrazorwebmatrixasp.net-webpages

提问by VoidKing

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.

我查看了大量资源,进行了大量研究,并尝试了许多“最佳猜测”以使用 WebMatrix 和 C#、网页一次访问单个元素,但是我尝试的任何内容都没有通过。

Consider a simple xml document that looks like this:

考虑一个如下所示的简单 xml 文档:

<root>
    <requisitionData>
        <element1>I am element 1</element1>
        <element2>I am element 2</element2>
    </requisitionData>
</root>

I know I can use a foreach loop, like so:

我知道我可以使用 foreach 循环,如下所示:

@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?

当然,这很好用。但是,如果我只是想将单个元素<element1>的值存储在字符串变量中呢?

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...):

我看过这里(下面的链接),但我无法理解这段代码的正面或反面(对我来说,它甚至看起来都不像 C#,但话说回来,我对解析 XML 还是很陌生……):

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b14ce4d1-77f1-420d-ad91-0989794a1d45/

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/b14ce4d1-77f1-420d-ad91-0989794a1d45/

I've also checked here: How to Get XML Node from XDocument

我也在这里检查过:How to Get XML Node from XDocument

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;

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?)

我收到的错误:无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Xml.Linq.XElement”。存在显式转换(您是否缺少演员表?)

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------------------------------

- - - - - - - - - - - - -更新 - - - - - - - - - - - - ------

I was able to get this to work:

我能够让它工作:

string element = doc.Element("root").Element("requisitionData").Element("element1").Value;

@element

However, one thing that concerns me about this approach is that .Elementselects the 'first' match, so in an xml document that looks like this:

但是,我对这种方法担心的一件事是.Element选择“第一个”匹配项,因此在如下所示的 xml 文档中:

<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>?

我如何访问第二次出现的<element1>

采纳答案by Darin Dimitrov

@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:

或使用 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:

如果您想<element1>从更新的示例中选择例如第二个节点:

string value = doc.XPathSelectElement("//requisitionData[2]/element1").Value;