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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:42:23+00:00 2026-06-04T17:42:23+00:00

I have an xml document that goes like this: <Menu> <Category name=Comida Rapida> <Food

  • 0

I have an xml document that goes like this:

<Menu>
    <Category name="Comida Rapida">
        <Food cocina="si">
            <Name>Haburguesa</Name>
            <Price>10</Price>
        </Food>
        <Food>
            <Name>Papas Fritas</Name>
            <Price>20</Price>
        </Food>
    </Category>
    <Category name="Bebidas">
        <Food>
            <Name>Pepsi</Name>
            <Price>30</Price>
        </Food>
        <Food cocina="si">
            <Name>Coca Cola</Name>
            <Price>40</Price>
        </Food>
    </Category>
</Menu>

What I want to do is go through each <Category> checking if the attribute is what I need, for example “Bebidas”, so the part I’m interested in is:

<Food>
    <Name>Pepsi</Name>
    <Price>30</Price>
</Food>
<Food cocina="si">
    <Name>Coca Cola</Name>
    <Price>40</Price>
</Food>

Now that I have this I want to do something similar to what I have done already:

First I want to print out all:

Pepsi 30
Coca Cola 40

And the I want to print out only the ones that food had the attribute cocina="si", so:

Coca Cola 40

So I have various questions:

First of all which approach to use, I am confused by the abundance of possible methods and implementations: XmlDocument, XmlReader, XmlTextReader, etc.

From this question I gather XmlDocument is the easier to use, that would be great, the simpler, the better as I am quite new to parsing Xml files as you can appreciate.

Now to the actual implementation, I have tried all sort of things with not much succes, I seem to be able to do some parts but not all together.

XmlNodeList elemList = doc.GetElementsByTagName("Category");
for (int i = 0; i < elemList.Count; i++)
{
    Console.WriteLine(elemList[i].InnerXml);
}

This will output:

<Food><Name>Haburguesa</Name><Price>10</Price></Food><Food><Name>Papas Fritas</Name><Price>20</Price></Food>
<Food><Name>Pepsi</Name><Price>30</Price></Food><Food><Name>Coca Cola</Name><Price>40</Price></Food> 

Which makes sense but now, how do I check if the category has the attribute name="cocina"?

I’m guessing something like this could help:

for (int j = 0; j < elemList[i].Attributes.Count; j++)
{
    //??                
}

But I can’t find something like MoveToAttribute() in XmlTextReader.

And then again, how do I check whether has the attribute cocina="si"?

  • 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-04T17:42:24+00:00Added an answer on June 4, 2026 at 5:42 pm

    I think LINQ to XML would be the easiest method here:

    You have to use XDocument class here. Create your document object using static methods XDocument.Parse(DOCUMENT) – to load document from string – or XDocument.Load(PATH) – to load a document from file with given path.

    After that you can easily find what you are looking for by a query like that:

    var doc = XDocument.Parse("<Menu> ... </Menu>");
    var results = doc.Descendants("Category")
                     .Where(cat => (string)cat.Attribute("name") == "Bebidas")
                     .SelectMany(cat => cat.Elements("Food"))
                     .Where(food => (string)food.Attribute("cocina") == "si")
                     .Select(food => string.Format("{0} {1}", food.Element("Name"), food.Element("Price"))).ToList();
    

    To make it more clear I’ll try to describe what that query does:

    1. Takes all elements named “Category” that are descendants of document root element
    2. Selects only that categories, which “name” attribute has value “Bebidas”
    3. Projects “Food” elements within that categories into one collection
    4. Selects “food” elements with “cocina” attribute value equal to “si”
    5. Projects result elements into a string formatted “name – price”
    • 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 looks like this. <foo> <bar type=artist/> Bob Marley
I have an XML document that matches our site navigation something like this: <page
I have a XML document that looks something like this: <?xml version=1.0 encoding=UTF-8?> <citizen>
I have a xml document that has a record set like this. <document> <row>
I have an xml document that looks something like this <?xml version=1.0 encoding=UTF-8?> <mapPoints>
I have an XML document that looks like this: <kmsg xmlns=http://url1 xmlns:env=url1 xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance xsi:schemaLocation=http://location
I have an xml document that looks like this. <?xml version=1.0?> <services> <service sn=1
I have a dilemma that goes like this. I have a string that represents
I have an XML document that I would like to update after it already
I have an XML document and a CSS file that goes with it, which

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.