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

  • Home
  • SEARCH
  • 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 8621743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:53:11+00:00 2026-06-12T06:53:11+00:00

I have an xml document that has 3 elements in this format: <Codes> <Code>

  • 0

I have an xml document that has 3 elements in this format:

<Codes>
    <Code>
        <CodeScenario>3</CodeScenario>
        <CodeScenario>4</CodeScenario>
        <CodeScenario>6</CodeScenario>
        <CodeScenario>7</CodeScenario>
        <CodeCategory>Category3</CodeCategory>
        <CodeID>043</CodeID>
        <CodeDescription>Description3</CodeDescription>
        <CodeType>PE</CodeType>
        </Code>
    <Code>
        <CodeCategory>Category2</CodeCategory>
        <CodeID>046</CodeID>
        <CodeDescription>Description2</CodeDescription>
        <CodeType>PE</CodeType>
    </Code>
    <Code>
        <CodeScenario>2</CodeScenario>
        <CodeCategory>Category1</CodeCategory>
        <CodeID>100</CodeID>
        <CodeDescription>Description1</CodeDescription>
        <CodeType>PR</CodeType>
    </Code>
</Codes>

I’d like to be able to use a linq query to tell me if a code element has a codetype based on ID. This is what I’ve come up with so far. It works but it’s ugly and I’d prefer to use dot notation if possible. Can anybody help me out?

public bool HasCodeType(string CodeType, string Code)
{
    var QueryResult = (from descendant in XMLdocument.Descendants("Code")
                       from type in descendant.Elements("CodeType")
                       where type != null &&
                             type.Value.Equals(CodeType)
                       select new { CID = descendant.Element("CodeID").Value }).Distinct();

    foreach (var result in QueryResult)
    {
        if (Code == result.CID)
            return true;
    }

    return false;
}
  • 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-12T06:53:12+00:00Added an answer on June 12, 2026 at 6:53 am

    It’s probably simplest to find the CodeType first, then go “up and down”:

    // Note camelCase for variable names, not PascalCase.
    var query = document.Descendants("CodeType")
                        .Where(type => type.Value == codeType)
                        .Select(type => (string) type.Parent.Element("CodeID"))
                        .Where(id => id == null)
                        .Distinct();
    

    The second Where clause is to avoid getting null values for CodeType elements with no sibling CodeID element.

    If all you’re doing with the query is checking for the presence of the given ID though, you don’t need to go to all that trouble:

    return document.Descendants("Code")
                   .Any(code => (string) code.Element("CodeType") == codeType &&
                                (string) code.Element("CodeID") == codeId));
    

    Really simple.

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

Sidebar

Related Questions

I have an XML document that has several <item> elements. Inside each, there might
I have a xml document that has a record set like this. <document> <row>
I have XML tag that has the content which is HTML document. <xml-tag> <!--
I have an xml document that looks like this. <foo> <bar type=artist/> Bob Marley
I have an XML document that matches our site navigation something like this: <page
This is a tough one to google for. I have an XML document that's
I have an XML document that I'm trying to search through. The Main structure
I have an XML document that I generate from an Entity Framework object. The
I have an XML document that I load in and try to search with
I have an XML document that includes XPath expressions that I need to use

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.