1

I am trying to learn how to search XML trees with LINQ using VB.net. I've found some very helpful posts for C#, but none for 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:

   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 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>
2
  • 2
    Try this: Dim inputLocation As String = xdocument.Descendants("Configurations").Descendants("process").First(Function(c) c.Element("name").Value.Equals("MyProcess1")).Element("inputLocation").Value.Trim() Commented Nov 29, 2011 at 21:19
  • Great :). I've added it as an answer. Commented Nov 29, 2011 at 21:25

1 Answer 1

3

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 :). I've inserted Equals() instead of = just in case, but both should work :).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.