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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:20:08+00:00 2026-05-15T15:20:08+00:00

I have the following: XDocument xdoc = XDocument.Load(C:\\myfile.xml); List<Tag> list = new List<Tag>(); foreach

  • 0

I have the following:

XDocument xdoc = XDocument.Load("C:\\myfile.xml");

List<Tag> list = new List<Tag>();

foreach (var tag in xdoc.Descendants("META"))

{

string name = tag.Attribute("name").Value;

string value = tag.Element("value").Value;

list.Add(new Tag { Name = name, Value = value, Score = score, Key = key });

}

but I need to only get the META elements under the element ARTICLE.

Can I add this to the linq query somehow?

The xml looks similar to this:

<DOCUMENT>
  <META name="aaa"/>
  <ARTICLE>
   <META name="bbb" value="708" score="0.58" key="6008"/>
  </ARTICLE>
</DOCUMENT>

Thanks for any suggestions

In the end I need to do something like the following:

XDocument xdoc = XDocument.Load(tr);
var meta = from el in xdoc.Descendants("ARTICLE").Elements("META")
where (string) el.Attribute("name") == "RAW"
select el;

List<Tag> list = new List<Tag>();
foreach (var tag in meta)
{
 string name = tag.Attribute("name").Value;
 string value = tag.Attribute("value").Value;
 string score = tag.Attribute("score").Value;
 string key = tag.Attribute("key").Value;

list.Add(new Tag { Name = name, Value = value, Score = score, Key = key });
}    

The reason for this is that I needed to match the attribute where the name was equal to RAW.

Please correct me if there’s a better way to do 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-15T15:20:09+00:00Added an answer on May 15, 2026 at 3:20 pm

    To find them anywhere in the document, use xdoc.Descendants("ARTICLE") to find all the ARTICLE elements, and then Elements("META") from there to find all the direct META children elements.

    In addition, you can perform the projection and the conversion to a list in the same query:

    var list = xdoc.Descendants("ARTICLE")
                   .Elements("META")
                   .Select(x => new Tag { Name = (string) x.Attribute("name"),
                                          Value = (string) x.Attribute("value"),
                                          Key = key })
                   .ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie to C# here.... I have the following code: var xdoc = XDocument.Parse(xml); var
If I have the following xml: XDocument xDocument = new XDocument( new XElement(RootElement, new
I have the following XML LINQ query from my XDocument. var totals = (from
I have the following XML which I load via XDocument.Load(uri) or XElement.Load(uri) . I
I have the following code : XDocument xResponse = XDocument.Parse(strXMLResponse); var vMyData = from
I have the following XML which I am trying to query with XDocument: <E2ETraceEvent
I have the following code const string xml = @<?xml version=1.0 encoding=utf-8 ?> <Revisions>
I have the following xml string: <report> <item id=4219 Co=6063 LastName=Doe FirstName=John/> <item id=2571
I have the following xml in an XDocument (confirmed by checking in the debugger)
I have an xml (called xdoc) file like the following: <Root> <ItemContainer> <Item> <Item>

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.