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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:31:13+00:00 2026-06-13T09:31:13+00:00

I have a weird problem and I tried everything regarding XDocument. I want to

  • 0

I have a weird problem and I tried everything regarding XDocument.

I want to get the value of “CategoryClass” nodes and populate the sub nodes in my own object of type “ListBoxClass”. But the LINQ query is not returning anything.

System.Xml.Linq.XNamespace lName = xDoc.Root.GetDefaultNamespace();
        System.Xml.Linq.XNamespace lANamespace = "http://schemas.datacontract.org/2004/07/KRefreshService";

        var lEle = from xml2 in xDoc.Element(lName + "GetCategoriesResponse").Element(lName + "GetCategoriesResult").Elements(lANamespace + "CategoryClass")
                                                    select new ListBoxClass 
                                                    {
                                                        Id =  (int)xml2.Element(lName + "ID"),
                                                        Name = xml2.Element(lName+ "CatName").ToString(),
                                                        Description = xml2.Element(lName + "CatDescription").ToString()
                                                    };

Here is the XML

<GetCategoriesResponse xmlns="http://tempuri.org/">
 <GetCategoriesResult xmlns:a="http://schemas.datacontract.org/2004/07/KRefreshService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:CategoryClass>
  <a:CatDescription>General questions regarding IQ. These questions will help you prepare for the interviews.</a:CatDescription>
  <a:CatName>IQ</a:CatName>
  <a:ID>1</a:ID>
</a:CategoryClass>
<a:CategoryClass>
  <a:CatDescription>This category will help you improve your general knowledge. It have information from all the different subjects.</a:CatDescription>
  <a:CatName>General Knowledge</a:CatName>
  <a:ID>2</a:ID>
</a:CategoryClass>
<a:CategoryClass>
  <a:CatDescription>If you feel that you computer science basics are slipping by your head as you involve more in technology. No problem! subscribe to this category.</a:CatDescription>
  <a:CatName>Computer Science</a:CatName>
  <a:ID>3</a:ID>
</a:CategoryClass>

  • 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-13T09:31:14+00:00Added an answer on June 13, 2026 at 9:31 am

    The problem is this part of the query:

    select new ListBoxClass 
    {
        Id =  (int)xml2.Element(lName + "ID"),
        Name = xml2.Element(lName+ "CatName").ToString(),
        Description = xml2.Element(lName + "CatDescription").ToString()
    };
    

    You’re using the wrong namespace here – those elements have the a prefix:

    <a:CatDescription>...</a:CatDescription>
    <a:CatName>IQ</a:CatName>
     <a:ID>1</a:ID>
    

    … so they’re using namespace http://schemas.datacontract.org/2004/07/KRefreshService". You’re using the namespace declared in the root element of the document instead.

    Also I expect you want the value of each XElement, which I tend to fetch by the explicit conversion to string. Calling ToString() will give you the indented XML for the element. So you want:

    select new ListBoxClass 
    {
        Id =  (int) xml2.Element(lANamespace + "ID"),
        Name = (string) xml2.Element(lANamespace + "CatName"),
        Description = (string) xml2.Element(lANamespace + "CatDescription")
    };
    

    (It’s not clear why you’re fully-qualifiying System.Xml.Linq.XNamespace by the way, or why you’ve got an l prefix for your variables… you could do a lot to make this code clearer, when you’ve made it work.)

    EDIT: The code that you said doesn’t work behaves fine for me:

    using System;
    using System.Linq;
    using System.Xml.Linq;
    
    public class Test
    {
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load("test.xml");
            XNamespace envelopeNs = doc.Root.GetDefaultNamespace();
            XNamespace resultNs = "http://schemas.datacontract.org/2004/07/KRefreshService";
    
            Console.WriteLine(doc.Element(envelopeNs + "GetCategoriesResponse")
                                 .Element(envelopeNs + "GetCategoriesResult")
                                 .Elements(resultNs + "CategoryClass")
                                  .Count());
        }
    }
    

    That prints 3. So if you’re having trouble with it, there must be something you haven’t told us.

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

Sidebar

Related Questions

I have a really weird problem. I tried everything to fix it and so
I have this weird problem with setting up cookies with PHP. Everything worked fine
I have a really weird problem. I have tried to use CSS to set
So I've been struggling on a rather weird problem. I've tried everything from display:inline-block
I have a weird problem with a Java Gregorian Calendar: SimpleDateFormat sdf = new
I have a weird problem. When I execute a query the data is not
I have a weird problem, so I thought I would ask and see if
I have a weird problem. I create this window, but its blank until i
I have a weird problem with my mysql query syntax. I have made a
I have another weird problem which I have not been able to solve in

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.