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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:37:35+00:00 2026-06-02T02:37:35+00:00

I am trying to parse an xml document that I have created. However xml.Descendants(value)

  • 0

I am trying to parse an xml document that I have created. However xml.Descendants(value) doesn’t work if value has certain characters (including space, which is my problem).

My xml is structured like this:

<stockists>
  <stockistCountry country="Great Britain">
    <stockist>
      <name></name>
      <address></address>
    </stockist>
  </stockistCountry>
  <stockistCountry country="Germany">
    <stockist>
      <name></name>
      <address></address>
    </stockist>
  </stockistCountry>
  ...
</stockists>

And my C# code for parsing looks like this:

string path = String.Format("~/Content/{0}/Content/Stockists.xml", Helper.Helper.ResolveBrand());
XElement xml = XElement.Load(Server.MapPath(path));

var stockistCountries = from s in xml.Descendants("stockistCountry")
                                select s;

StockistCountryListViewModel stockistCountryListViewModel = new StockistCountryListViewModel
{
    BrandStockists = new List<StockistListViewModel>()
};

foreach (var stockistCountry in stockistCountries)
{
    StockistListViewModel stockistListViewModel = new StockistListViewModel()
    {
        Country = stockistCountry.FirstAttribute.Value,
        Stockists = new List<StockistDetailViewModel>()
    };

    var stockist = from s in xml.Descendants(stockistCountry.FirstAttribute.Value) // point of failure for 'Great Britain'
                   select s;

    foreach (var stockistDetail in stockist)
    {
         StockistDetailViewModel stockistDetailViewModel = new StockistDetailViewModel
         {
             StoreName = stockistDetail.FirstNode.ToString(),
             Address = stockistDetail.LastNode.ToString()
         };

         stockistListViewModel.Stockists.Add(stockistDetailViewModel); 
     }

     stockistCountryListViewModel.BrandStockists.Add(stockistListViewModel);
 }

 return View(stockistCountryListViewModel);

I am wondering if I am approaching the Xml parsing correctly, whether I shouldn’t have spaces in my attributes etc? How to fix it so that Great Britain will parse

  • 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-02T02:37:37+00:00Added an answer on June 2, 2026 at 2:37 am

    However xml.Descendants(value) doesn’t work if value has certain characters

    XElement.Descendants() expects an XName for the tag, not for the value.

    And XML tags are indeed not allowed to contain spaces.

    Your sample XML however only contains a value for an attribute, and the space there is fine.

    Update:

    I think you need

    //var stockist = from s in xml.Descendants(stockistCountry.FirstAttribute.Value) 
    //              select s;
    
    var stockists = stockistCountry.Descendants("stockist");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse a document that is formatted similar to Xml (but
I'm trying to parse and load an XML document, however I'm getting this exception
I am trying to parse some XML that looks similar to this: <document> <headings>
I'm trying to parse an XML document on Android 2.3.3, but it seems that
I have an xml document like the below one that I am trying to
Possible Duplicate: Parse XML Elements with LINQ I have an XML document that looks
I have the following XML document that I have to parse using python's minidom:
So I'm trying to parse the following XML document with C#, using System.XML: <root
Trying to do a simple parse of an XML document. What's the easiest way
I am trying to parse some XML i have retrieved via e4x in an

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.