Why is XML serialization always returning NULL C#

Viewed 17

I have the following code which I think should work but it just returns NULL on all 4 properties:

    [XmlRoot("ArrayOfSomething")]
    public class ArrayOfSomething
    {
        [XmlAttribute()]
        public string Id { get; set; }
        [XmlAttribute()]
        public string Bool { get; set; }
        [XmlAttribute()]
        public string Title { get; set; }
        [XmlAttribute()]
        public string Something { get; set; }
    }

 public class NamespaceIgnorantXmlTextReader : XmlTextReader
        {
            public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader) : base(reader) { }

            public override string NamespaceURI
            {
                get { return ""; }
            }
        }

public void Run()
        {
            

                XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfSomething));
                StringReader sr = new StringReader("<ArrayOfSomething xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ><Item><Id>0</Id><Bool>false</Bool><Title>Some text</Title><Something/></Item><Item><Id>0</Id><Bool>false</Bool><Title>Some text 2</Title><Something/></Item><Item><Id>0</Id><Bool>false</Bool><Title>Some text 3</Title><Something/></Item><Item><Id>0</Id><Bool>false</Bool><Title>Some text 4</Title><Something/></Item></ArrayOfSomething>");

                NamespaceIgnorantXmlTextReader XMLWithoutNamespace = new NamespaceIgnorantXmlTextReader(sr);


                var s = (ArrayOfSomething)serializer.Deserialize(XMLWithoutNamespace);
}

And to make it easier for you to read the xml:

<ArrayOfSomething xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Item>
        <Id>0</Id>
        <Bool>false</Bool>
        <Title>Some text</Title>
        <Something/>
    </Item>
    <Item>
        <Id>0</Id>
        <Bool>false</Bool>
        <Title>Some text 2</Title>
        <Something/>
    </Item>
    <Item>
        <Id>0</Id>
        <Bool>false</Bool>
        <Title>Some text 3</Title>
        <Something/>
    </Item>
    <Item>
        <Id>0</Id>
        <Bool>false</Bool>
        <Title>Some text 4</Title>
        <Something/>
    </Item>
</ArrayOfSomething>

Why is s always returning null for all properties? I have used XMLAttribute on all properties and all methods are public. I started by using the XSD way but it gave me the same result so I tried to make it as above to do it in an easy way.

1 Answers

Xml Serilaize with arrays assume two tags . By placing [XmlElement] the default is changed to one tag. You also cannot have an array at root so in you example xml You need a separate class for the root. If the example was not at root you would not need two classes as in my example you can use one. When using one you need [XmlArray] (the parent) [XmlArrayItem] (the child).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;


namespace ConsoleApplication40
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfSomething));
            ArrayOfSomething arrayOfSometime = (ArrayOfSomething)serializer.Deserialize(reader);
 
        }
    }
    [XmlRoot("ArrayOfSomething")]
    public class ArrayOfSomething
    {
        [XmlElement()]
        public List<Item> Item { get; set; }
    }
    public class Item
    {
        [XmlAttribute()]
        public string Id { get; set; }
        [XmlAttribute()]
        public string Bool { get; set; }
        [XmlAttribute()]
        public string Title { get; set; }
        [XmlAttribute()]
        public string Something { get; set; }
    }
 
}
Related