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 666557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:49:53+00:00 2026-05-13T23:49:53+00:00

I’m just learning LINQ and in particular LINQ to XML, and I’ve written up

  • 0

I’m just learning LINQ and in particular LINQ to XML, and I’ve written up a query that works but I’m wondering if I’m doing it a bit round-a-bout. Can the code be improved?

I have an XDocument:

<SomeDocument>
    <Prop1> val1 </Prop1>
    <Prop2> val2 </Prop2>
    <Prop3> val3 </Prop3>
</SomeDocument>

But Prop1, Prop2, and Prop3 may not be there. There may be other XDocuments which I’ll be parsing with the same code, that have different properties altogether. But I’m only interested in the XDocument if it has Prop1 or both Prop1 and Prop2.

var query = from n in xml.Elements()
                    where n.Name == "Prop1" || n.Name == "Prop2"
                    select new {n.Name, n.Value};

string prop1 = null;
string prop2 = null;

foreach (var n in query)
{
    if (n.Name == "Prop1") prop1 = n.Value;
    if (n.Name == "Prop2") prop2 = n.Value;
}

if (string.IsNullOrEmpty(prop1)) { //error } 
if (string.IsNullOrEmpty(prop2)) { DoMethod1(prop1); }
else { DoMethod2(prop1, prop2); }

The code after the query seems way too long to me, though I’m not sure if there’s a better way of doing what I’m trying to do. Find 1 or 2 explicit Nodes, and call relevant methods depending on what (if any) nodes are found.

  • 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-05-13T23:49:53+00:00Added an answer on May 13, 2026 at 11:49 pm

    personally I use:

            var element = xml.Element("prop1");
            if (element!= null)
            {
                //The element exists, now do things on it!
                if(string.IsNullOrEmpty(element.Value)) {DoMe(element.Value);}
            }
    

    if you have named properties (elements) that you know, you can just name them and it will be returned. This also saves extra looping (in your code at least)

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.