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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:52:21+00:00 2026-06-03T11:52:21+00:00

I am working on XmlElement in c#. I have an XmlElement . The source

  • 0

I am working on XmlElement in c#. I have an XmlElement. The source of the XmlElement will look like the sample below.

Sample:

<data>
    <p>hello all
        <strong>
            <a id="ID1" href="#" name="ZZZ">Name</a>
        </strong>
    </p>
    <a id="ID2" href="#" name="ABC">Address</a>
</data>

I have to loop through the above XML to Search for element name a. I also want to extract the ID of that element into a variable.

Basically I want to get the ID attribute of the element <a>. It may occur as a child element or as a separate parent.

Can any one help how it can be done.

  • 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-03T11:52:23+00:00Added an answer on June 3, 2026 at 11:52 am

    Since you’re using C# 4.0 you could use linq-to-xml like this:

    XDocument xdoc = XDocument.Load(@"C:\Tmp\your-xml-file.xml");
    foreach (var item in xdoc.Descendants("a"))
    {
       Console.WriteLine(item.Attribute("id").Value);
    }
    

    Should give you the element a regardless of where it is in the hierarchy.


    From your comment, for code that only uses the XmlDocument and XmlElement classes the equivalent code would be:

    XmlDocument dd = new XmlDocument();
    dd.Load(@"C:\Tmp\test.xml");
    XmlElement theElem = ((XmlElement)dd.GetElementsByTagName("data")[0]);
    //         ^^ this is your target element 
    foreach (XmlElement item in theElem.GetElementsByTagName("a"))//get the <a>
    {
        Console.WriteLine(item.Attributes["id"].Value);//get their attributes
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on importing XML into an iPad Core Data application. I have
So, deserializing is working however I have some xml like this: <File> <Stuff>stuff</Stuff> <Devices>
I'm working on a WP7 app. The app will have a couple of XML
I am working with multiple XElement objects to provide some user-specified data for several
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
Working on game where plates will be falling from top to bottom. Some plates
I'm working on a webservices client app and I have it mostly working. I
I have created new class to read the data from xml file, which looks
I have a situation where I need to generate the below XML dynamically in
I'm working with a complicated xml schema, for which I have created a class

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.