【发布时间】:2011-11-29 20:43:13
【问题描述】:
我正在尝试学习如何使用 VB.net 使用 LINQ 搜索 XML 树。我发现了一些对 C# 很有帮助的帖子,但对 VB.net 没有。
- How would I use LINQ to XML to get the value from example XML
- Getting a set of elements using linq
- How to get elements value with Linq To XML
我想获取 name = "MyProcess1" 的进程的输入位置根据上面的示例链接,我一直在尝试这样的代码:
Dim inputLocation As String = xdocument.Descendants("Configurations").Descendants("process").First(Function(c) c.Element("name").Value = "MyProcess1").Element("inputLocation").Value
但是代码没有返回任何值。这是xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configurations>
<process>
<name>MyProcess1</name>
<inputLocation> inputPath
</inputLocation>
<outputLocation> outputPath1
</outputLocation>
</process>
<process>
<name>MyProcess2</name>
<inputLocation> inputPath2
</inputLocation>
<outputLocation>outputPath2
</outputLocation>
</process>
</Configurations>
【问题讨论】:
-
试试这个:Dim inputLocation As String = xdocument.Descendants("Configurations").Descendants("process").First(Function(c) c.Element("name").Value.Equals ("MyProcess1")).Element("inputLocation").Value.Trim()
-
它有效。感谢您的帮助
-
太棒了:)。我已将其添加为答案。