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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:19:24+00:00 2026-05-14T02:19:24+00:00

I am having xml with following structure <ruleDefinition appId=3 customerId = acf> <node alias=element1

  • 0

I am having xml with following structure

<ruleDefinition appId="3" customerId = "acf">
    <node alias="element1" id="1" name="department">
        <node alias="element2" id="101" name="mike" />
        <node alias="element2" id="102" name="ricky" />
        <node alias="element2" id="103" name="jim" />
    </node>
</ruleDefinition>

Here nodes are differentiated using alias and not with node tag. As you can see top level node element1 has same node name “node” as element2. I want to parse this XML based on attribute alias.

What should be the Linq-To-Xml code (using C#)to acheive this?

  • 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-14T02:19:24+00:00Added an answer on May 14, 2026 at 2:19 am

    This is a method of parsing the data structure into items that contain an Element1 string property and an IEnumerable Element2 property, which contains your multiple pieces of element2 data.

    string xml = @"<ruleDefinition appId=""3"" customerId = ""acf""> 
        <node alias=""element1"" id=""1"" name=""department"">
            <node alias=""element2"" id=""101"" name=""mike"" /> 
            <node alias=""element2"" id=""102"" name=""ricky"" />
            <node alias=""element2"" id=""103"" name=""jim"" />
        </node>
        </ruleDefinition>";
    
    XDocument document = XDocument.Parse(xml);
    
    var query = from node in document.Descendants("node")
                where node.Attribute("alias").Value == "element1"
                select new
                {
                    Element1 = new
                               {
                                   Id = node.Attribute("id").Value,
                                   Name = node.Attribute("name").Value
                               },
    
                    Element2 = from n in node.Descendants("node")
                               where n.Attribute("alias").Value == "element2"
                               select new
                               {
                                   Id = n.Attribute("id").Value,
                                   Name = n.Attribute("name").Value
                               }
                };
    
    foreach (var item in query)
    {            
        Console.WriteLine("{0}\t{1}", item.Element1.Id, item.Element1.Name);
    
        foreach (var element2 in item.Element2)
            Console.WriteLine("\t{0}\t{1}", element2.Id, element2.Name);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having the problem that I cannot select a specific XML node which
In spring you can initialize a bean by having the applicationContext.xml invoke a constructor,
I am having problems manually looping through xml data that is received via an
I'm having a problem writing Norwegian characters into an XML file using C#. I
I've been having tons of problems getting the non-xml configuration for Castle Windsor set
I'm having the problem described here: http://groups.google.com/group/microsoft.public.xml.soap/browse_thread/thread/029ee5b5d4fa2440/0895d73c5c3720a1 I am consuming a Web Service using
I'm using the XML data source feature in Reporting Services 2005 but having some
I've seen a fair share of ungainly XML->JSON code on the web, and having
I have normally hand written xml like this: <tag><?= $value ?></tag> Having found tools
I am trying to export a Ruby framework via XML-RPC. However I am having

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.