简体   繁体   English

使用VB.net(不是C#)使用LINQ访问XML元素

[英]Accessing XML elements with LINQ using VB.net (not C#)

I am trying to learn how to search XML trees with LINQ using VB.net. 我正在尝试学习如何使用VB.net使用LINQ搜索XML树。 I've found some very helpful posts for C#, but none for VB.net 我找到了一些非常有用的C#帖子,但是没有一个用于VB.net

I want to get the inputlocation for the process where name = "MyProcess1" Based on the example links above, I have been trying code like this: 我想获取name =“MyProcess1”的进程的inputlocation基于上面的示例链接,我一直在尝试这样的代码:

   Dim inputLocation As String = xdocument.Descendants("Configurations").Descendants("process").First(Function(c) c.Element("name").Value = "MyProcess1").Element("inputLocation").Value

But the code is not returning any values. 但是代码没有返回任何值。 here is the xml 这是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>

Try this: 尝试这个:

Dim inputLocation As String = xdocument.Descendants("Configurations").Descendants("process").First(Function(c) c.Element("name").Value.Equals("MyProcess1")).Element("inputLocation").Value.Tri‌​m();

It basically just trims the \\n character from the end of the value returned :). 它基本上只是从返回值的末尾修剪\\ n字符:)。 I've inserted Equals() instead of = just in case, but both should work :). 我插equals()方法 ,而不是为了以防万一=,但都应该工作:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM