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 want to write a XML file as below: <?xml version=1.0 encoding=UTF-8?> <books xmlns:xsd=http://www.w3.org/2001/XMLSchema
I have a XML File in the following format: <?xml version='1.0' encoding='UTF-8'?> <entry xmlns='http://www.w3.org/2005/Atom'
How can I create xml file with this format using c#? <?xml version=1.0 encoding=utf-8
I have a custom XML file format which can contain blocks of code within
I am looking for a reference describing precisely the XML project file format used
I have a datetime coming back from an XML file in the format: 20080916
What format do I use for Date/Time when writing to an XML file using
Our product has the requirement of exporting its native format (essentially an XML file)
I get an XML file From a web service. Now I want to get
I have an Xml file. It's format is as follows: ControlType > Content >

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.