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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:48:16+00:00 2026-05-24T10:48:16+00:00

XML file format: <?xml version=1.0 encoding=UTF-8?> <urlset> <url> <loc>element1</loc> <changefreq>daily</changefreq> <priority>0.2</priority> </url> <url> <loc>element2</loc>

  • 0

XML file format:

<?xml version="1.0" encoding="UTF-8"?>
    <urlset>    
        <url>
            <loc>element1</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
        <url>
            <loc>element2</loc>
            <changefreq>daily</changefreq>
            <priority>0.2</priority>
        </url>
    <urlset>

I want to select all “loc” nodes (element1, element2), but this not work!!!

 foreach (XElement item in document.Elements("url").Descendants("loc")) // Change into what?
 {
      urlList.Add(item.Value);
 }
  • 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-24T10:48:18+00:00Added an answer on May 24, 2026 at 10:48 am

    I suspect the problem is that you’re going from document.Elements("url") instead of document.Root.Elements("url")… so it’s looking for a root element of url, which doesn’t exist.

    Try this:

    List<string> urlList = doc.Root.Elements("url")
                                   .Elements("loc")
                                   .Select(x => (string) x)
                                   .ToList();
    

    Note that I haven’t used Descendants here, as the loc elements are all directly beneath url elements anyway.

    Another alternative you could use if the only loc elements are the right ones anyway, is just:

    List<string> urlList = doc.Descendants("loc")
                              .Select(x => (string) x)
                              .ToList();
    

    (I’m assuming urlList was empty beforehand… for this sort of situation I like to use LINQ for the whole operation and eliminate foreach loops that are just adding to a collection.)

    EDIT: The code works for me. Here’s a short but complete program:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;
    
    class Test
    {
        static void Main(string[] args)
        {
            string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
        <urlset>    
            <url>
                <loc>element1</loc>
                <changefreq>daily</changefreq>
                <priority>0.2</priority>
            </url>
            <url>
                <loc>element2</loc>
                <changefreq>daily</changefreq>
                <priority>0.2</priority>
            </url>
        </urlset>";
    
            XDocument doc = XDocument.Parse(xml);
            List<string> urlList = doc.Root
                                      .Elements("url")
                                      .Elements("loc")
                                      .Select(x => (string) x)
                                      .ToList();
            Console.WriteLine(urlList.Count);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a XML File in the following format: <?xml version='1.0' encoding='UTF-8'?> <entry xmlns='http://www.w3.org/2005/Atom'
I have an XML file with this format: <?xml version=1.0 encoding=utf-8 ?> <OpacResult> <valueObjects
I have an XML file with this format: <?xml version=1.0 encoding=utf-8 ?> <OpacResult> <valueObjects
I have an XML file with the following format: <?xml version=1.0 encoding=UTF-8?> <Items> <Item
I have got below xml format file called ResourceData.xml . <?xml version=1.0 encoding=utf-8 ?>
How can I create xml file with this format using c#? <?xml version=1.0 encoding=utf-8
I have an XML file with this structure: <?xml version=1.0 encoding=utf-8 ?> <Photobank> <Landowner
I have an XML document in this format: <?xml version=1.0 encoding=utf-8 ?> <SupportedServices> <Service>
Say I have an XML file which looks like this: <?xml version=1.0 encoding=UTF-8?> <Project
I want to create a XML file in csharp like the below format pls

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.