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();
        }
    }
}
SteveC
  • 15,808
  • 23
  • 102
  • 173
angrifel
  • 724
  • 2
  • 6
  • 19
  • 3
    Random thought - have you tried `[XmlElement(Namespace="")]` on the properties? – Marc Gravell Oct 17 '11 at 15:33
  • @Marc, that's exactly what needs to be done. You can post this as answer. – Darin Dimitrov Oct 17 '11 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 – Marc Gravell Oct 17 '11 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. – Darin Dimitrov Oct 17 '11 at 16:03

1 Answers1

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; }
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Works as indicated. I incorrectly assumed that leaving the namespace unsetted was that the same as setting it to "". Obviously, I was wrong. – angrifel Oct 17 '11 at 16:15
  • 2
    @Felipe "not stated" means "use the member-name for the element-name, and inherit the namespace from the parent" – Marc Gravell Oct 17 '11 at 16:39