4

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();
        }
    }
}
4
  • 3
    Random thought - have you tried [XmlElement(Namespace="")] on the properties? Commented Oct 17, 2011 at 15:33
  • @Marc, that's exactly what needs to be done. You can post this as answer. Commented Oct 17, 2011 at 15:38
  • @Darin - I was on mobile, and not in a position to validate, hence I didn't post as an answer (I like to be reasonably confident in answers). BTW, looks like I only have a few months left now before you overtake me ;p Commented Oct 17, 2011 at 15:56
  • @MarcGravell, reputation is not important. It's the quality of the answers that really matters. And yours have always been of a very high quality and allowing many developers to learn from. Commented Oct 17, 2011 at 16:03

1 Answer 1

15

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; }
Sign up to request clarification or add additional context in comments.

2 Comments

Works as indicated. I incorrectly assumed that leaving the namespace unsetted was that the same as setting it to "". Obviously, I was wrong.
@Felipe "not stated" means "use the member-name for the element-name, and inherit the namespace from the parent"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.