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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:52:21+00:00 2026-06-17T22:52:21+00:00

I need to deserialize the xml below. I want to use xmlserializer because I

  • 0

I need to deserialize the xml below. I want to use xmlserializer because I am (more) familiar with it.
I believe this xml is not constructed correctly however I cannot change it.
The below represents a list of category objects. When I try to deserialize using

xmlserializer(typeof(List<Category>))

I get this error: “categories xmlns=” is not expected”

<?xml version="1.0" encoding="utf-8" ?>
<categories>
  <category id="16" name="Exports" parent_id="13"/>
  <category id="17" name="Imports" parent_id="13"/>
  <category id="3000" name="Income Payments & Receipts" parent_id="13"/>
  <category id="125" name="Trade Balance" parent_id="13"/>
  <category id="127" name="U.S. International Finance" parent_id="13"/>
</categories>

I don’t mind making some kind of dummy class to deserilize these if that is what I have to do.
Here is my Category Class

[XmlType("category")]
 public class Category
 {


    [XmlAttribute("id")]
    public int ID { get; set; }

    [XmlAttribute("parent_id")]
    public int ParentID { get; set; }

    [XmlAttribute("name")]
    public string Name { get; set; }
}

My code:

    XmlSerializer serializer = new XmlSerializer(typeof(List<Category>));
    StringReader reader = new StringReader(xml);
    List<Category> obj = null;

    using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader))
    {
        obj = (List<Category>)serializer.Deserialize(xmlReader);
    }
        return obj;
  • 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-17T22:52:23+00:00Added an answer on June 17, 2026 at 10:52 pm

    You can just pass in the XmlRootAttribute into the serializer for the “categories” part.

    BUT… you must remove the “&” from your xml because its not valid

    XmlSerializer serializer = new XmlSerializer(typeof(List<Category>), new XmlRootAttribute("categories"));
    
    using (FileStream fileStream = new FileStream(@"C:\Test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        var test = serializer.Deserialize(fileStream);
    }
    

    enter image description here

    Here is your method working with a String.Replace to sort out the “&”

        private List<Category> GetCategories(string xmlData)
        {
            List<Category> obj = null;
            XmlSerializer serializer = new XmlSerializer(typeof(List<Category>), new XmlRootAttribute("categories"));
            StringReader reader = new StringReader(xmlData.Replace("&","&amp;"));
            using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader))
            {
                obj = (List<Category>)serializer.Deserialize(xmlReader);
            }
            return obj;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following XML: <a> <b>2</b> <c></c> </a> I need to deserialize this xml
I have this Xml file, and I need to Deserialize it back to a
I need to deserialize xml file to List or Array then enlarge this List
I have this XML, and i need deserialize but type is incorrect. <Consumer> <Sex
When deserializing a document using XmlSerializer are not deserialized correctly Document <?xml version=\1.0\?> <ns0:ElementA
I need to deserialize a XML file to a object. Following is the XML
I am trying to deserialize a XML-file. I need to check if the XML-file
I have given XML that I can't change and I need to deserialize it
I have the following XML message and I need to deserialize it <xs:complexType name=DataType>
I have some Xml that I need to deserialize into an object. The Xml

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.