Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7858245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:20:24+00:00 2026-06-02T21:20:24+00:00

I am working with LINQ to XML in VB (although answers in C# are

  • 0

I am working with LINQ to XML in VB (although answers in C# are great, too). First off- my code works, so this is not critical. However, I can’t help but think that what I’m doing with conditional logic should already be doable using the LINQ query itself.

Here’s what I’ve got:

        'Get the Description elements in the ResultData section where the value is "Op Code"
        Dim opCodes As List(Of XElement) = (From c In xdoc.Descendants("ResultData").Descendants("Description")
                 Where c.Value = "Op Code"
                 Select c).ToList()

        If opCodes.Count > 0 Then
            'Get the sibling XElement of the first "Description" XElement (there should be only one) by using the parent
            sOperator = Convert.ToString(((From c In opCodes
                     Select c).FirstOrDefault().Parent).<Value>.Value)
        Else
            sOperator = "Not Available"
        End If

Please don’t be too critical of my method of locating the sibling nodes- the XML files are produced by a third-party testing device and this is the only generic way to get the values I need. What I’m really looking for here is a better way to handle the If block.

Any thoughts offered on this will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:20:26+00:00Added an answer on June 2, 2026 at 9:20 pm

    It sounds like you want (C#):

    string operator = xdoc.Descendants("ResultData")
                          .Descendants("Description")
                          .Where(x => x.Value == "Op Code")
                          .Select(x => x.Parent.Element("Value").Value)
                          .FirstOrDefault() ?? "Not available";
    

    That assumes I’ve understood you correctly, that you’re just trying to get the text within the sibling “Value” element.

    I don’t think the null-coalescing (??) operator exists in VB, but you could take everything as far as FirstOrDefault, and then just have an “if it’s nothing, set it to Not Available” check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first day working with XML in the context of XPath, XQuery,
I am working with a method using LINQ to XML to return a string.This
Order by descending is not working on LINQ to Entity In the following Query
Hello everybody my linq to sql selection does not working on dateTime field for
I am using Linq To Xml to create an Xml file from DataSet. This
I have this code, but can't get it all working. I am trying to
I'm working with xml market data and I got the almost inverse of this
im working with xml and linq. I have 2 xml files both contain ID
I am working at Windows Phone 7 C#, XAML, XML and LINQ programming. I
I am working with LINQ to XML in C# and I need to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.