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

  • Home
  • SEARCH
  • 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 6687713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:19:16+00:00 2026-05-26T05:19:16+00:00

When deserializing a document using XmlSerializer are not deserialized correctly Document <?xml version=\1.0\?> <ns0:ElementA

  • 0

When deserializing a document using XmlSerializer are not deserialized correctly

Document

<?xml version=\"1.0\"?>
<ns0:ElementA xmlns:ns0=\"urn:some-namespace\">
    <Prop1>Some Value</Prop1>
    <Prop2>Some other value</Prop2>
</ns0:ElementA>

Class

[XmlRoot(Namespace = "urn:some-namespace")]
public class ElementA
{
    [XmlElement]
    public string Prop1 { get; set; }

    [XmlElement]
    public string Prop2 { get; set; }
}

Both Prop1 and Prop2 are null at the end of the deserialization.

I cannot change the structure of the document to get rid of the namespace so I need to handle the deserialization properly on my side.

The document has been simplified for the purpose of reproduction

How should I set the attributes on the ElementA to handle the deserialization correctly??

— Here is the full code for reproducing the problem —

namespace ConsoleApplication1
{
    using System;
    using System.IO;
    using System.Xml.Serialization;

    public class Program
    {
        [XmlRoot(Namespace = "urn:some-namespace")]
        public class ElementA
        {
            [XmlElement]
            public string Prop1 { get; set; }

            [XmlElement]
            public string Prop2 { get; set; }
        }

        static void Main(string[] args)
        {
            var element =
                "<?xml version=\"1.0\"?>" + Environment.NewLine +
                "<ns0:ElementA xmlns:ns0=\"urn:some-namespace\">" + Environment.NewLine+                    "    <Prop1>Some Value</Prop1>" + Environment.NewLine +
                "    <Prop2>Some other value</Prop2>" + Environment.NewLine +
                "</ns0:ElementA>";

            XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(ElementA) })[0];
            ElementA result;

            using (var reader = new StringReader(element))
            {
                result = serializer.Deserialize(reader) as ElementA;
            }

            Console.WriteLine("Prop1: " + result.Prop1);
            Console.WriteLine("Prop2: " + result.Prop2);
            Console.ReadKey();
        }
    }
}
  • 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-26T05:19:16+00:00Added an answer on May 26, 2026 at 5:19 am

    Originally posted as a comment, since I haven’t verified it, but:

    <Prop1>Some Value</Prop1>
    

    is not the same as

    <ns0:Prop1>Some Value</ns0:Prop1>
    

    So to get it working, you probably just need:

    [XmlElement(Namespace="")]
    public string Prop1 { get; set; }
    
    [XmlElement(Namespace="")]
    public string Prop2 { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem deserializing some XML; the XML supplied by a third party
I have an XML document that I am deserializing (VB .NET 3.5 Framework), changing
Having problems deserializing some xml into an object in C#. The error that I
I have some XML that I am consuming and deserializing. <Foo> <Bars Baz=9> <Bar>...</Bar>
So, deserializing is working however I have some xml like this: <File> <Stuff>stuff</Stuff> <Devices>
I have some JSON that I am deserializing using Gson. { resp: { posts:
I'm deserializing a class called Method using .NET Serialization. Method contains a list of
I'm having issues deserializing certain Guid properties of ORM-generated entities using protobuf-net. Here's a
I'm trying to catch a ClassCastException when deserializing an object from xml. So, try
I am serializing some C structure to string and than deserializing it with strtok()

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.