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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:33:44+00:00 2026-06-04T14:33:44+00:00

Possible Duplicate: Use Linq to Xml with Xml namespaces I have an xml: <?xml

  • 0

Possible Duplicate:
Use Linq to Xml with Xml namespaces

I have an xml:

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">
<Global>
   <Items>
      <Item text="Main text" key="1">
         <Item text="Some text" key="1.1"/>
         <Item text="Other text" key="1.2"/>
         <Item text="Text" key="1.3"/>
      </Item>
      <Item text="Main text 2" key="2">
         <Item text="Some text" key="2.1"/>
         <Item text="Other text" key="2.2"/>
         <Item text="Text" key="2.3"/>
      </Item>
   </Items>
</Global>
</string>

I need to get items inside Item (like with keys 2.1,1.1 etc).

I have this code:

var xml = GetXmlFromWeb(service.ServiceLink, true);
                var stringXml = new StringReader(xml); // Load data from web)
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreWhitespace = true;
                var reader = XmlTextReader.Create(stringXml, settings);

                XDocument docWithDecode = XDocument.Load(reader);
                IEnumerable<XElement> elements = docWithDecode.Root.Descendants("Item");

As result i got Count = 0.

Thats interesting, if i write IEnumerable<XElement> elements = docWithDecode.Root.Descendants(); i got the elemets, but the sructure looks like mess, data was dublicated, each item has namespace refference etc. So something is wrong here.

Can somebody help me to write the working code? Thanks!

  • 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-04T14:33:45+00:00Added an answer on June 4, 2026 at 2:33 pm

    As I mentioned in the comments and L.B. mention in his answer, your .Count() == 0 problem is due to the lack of a namespace. See this answer that I linked above to explain it in more details, but effectively your problem is you are searching for an element call “Item” but in fact what you are actually trying to find is namespace + “Item”. You just need to add the namespace to the query.

    After re-reading your question, I just noticed a problem with your XML and your query. You have an node with several child nodes with the same name. If you use Descendants(), you are going to select all 8, rather than the 6 you specified in your question.

    To only select the child <Item> Nodes, you need to modify your query since Descendants() will select anything that matches. You actually need to make use of the .Elements() extension method and select each level specifically.

    XNamespace ns = "http://tempuri.org/";
    IEnumerable<XElement> elements = docWithDecode.Root
                                                  .Elements(ns + "Global")
                                                  .Elements(ns + "Items")
                                                  .Elements(ns + "Item")
                                                  .Elements(ns + "Item");
    

    You can also use Descendants(ns + "Items") instead of the first 2 Elements() in the query since it doesn’t repeat inside itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I use LINQ Contains(string[]) instead of Contains(string) Suppose I have
Possible Duplicate: Can I use predefined namespaces when loading an XDocument? I have following
Possible Duplicate: Use LINQ to read all nodes from XML I am trying to
Possible Duplicate: How can I use a string with the same name of an
Possible Duplicate: Use of class definitions inside a method in Java Can we have
Possible Duplicate: Why use iterators instead of array indices? string::iterator it; for (it =
Possible Duplicate: Use Visual Studio web.config transform for debugging I have an asp.net application
Possible Duplicate: When to use .First and when to use .FirstOrDefault with LINQ? What
Possible Duplicate: LINQ to append to a StringBuilder from a String[] Forgive my functional
Possible Duplicate: Concat all strings inside a List<string> using LINQ I am using C#

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.