1

I'm trying to deserialize an XML into a C# Object (containing an array of objects)

Here are the classes:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1015")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("I-collection", Namespace = "urn:xtk:queryDef", IsNullable = false)]
public partial class MyCollection
{

    private List<I> itemsField;

    public MyCollection()
    {
        this.itemsField = new List<I>();

    }

    [System.Xml.Serialization.XmlElementAttribute("I", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    public List<I> Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1015")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class I
{

    private string EMAILField;

    private string FIRST_NAMEField;

    private string INDIVIDUAL_KEYField;

    private string LANGUAGE_CODEField;

    private string LAST_NAMEField;

    private string USERNAMEField;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string EMAIL
    {
        get
        {
            return this.EMAILField;
        }
        set
        {
            this.EMAILField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string FIRST_NAME
    {
        get
        {
            return this.FIRST_NAMEField;
        }
        set
        {
            this.FIRST_NAMEField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string INDIVIDUAL_KEY
    {
        get
        {
            return this.INDIVIDUAL_KEYField;
        }
        set
        {
            this.INDIVIDUAL_KEYField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string LANGUAGE_CODE
    {
        get
        {
            return this.LANGUAGE_CODEField;
        }
        set
        {
            this.LANGUAGE_CODEField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string LAST_NAME
    {
        get
        {
            return this.LAST_NAMEField;
        }
        set
        {
            this.LAST_NAMEField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string USERNAME
    {
        get
        {
            return this.USERNAMEField;
        }
        set
        {
            this.USERNAMEField = value;
        }
    }
}

Trying to deserialize

MyCollection result = Serialization.Deserialize<MyCollection>(xmle.OuterXml);

where xmle.OuterXml is

<I-collection xmlns="urn:xtk:queryDef">
  <I EMAIL="[email protected]" FIRST_NAME="" INDIVIDUAL_KEY="5" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="UPed" />
  <I EMAIL="[email protected]" FIRST_NAME="" INDIVIDUAL_KEY="6" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="GO" />
  <I EMAIL="[email protected]" FIRST_NAME="" INDIVIDUAL_KEY="7" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="ir0" />
  <I EMAIL="[email protected]" FIRST_NAME="" INDIVIDUAL_KEY="8" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="UPlqfa" />
  <I EMAIL="[email protected]" FIRST_NAME="" INDIVIDUAL_KEY="9" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="jon17" />
  <I EMAIL="[email protected]" FIRST_NAME="" INDIVIDUAL_KEY="10" LANGUAGE_CODE="N/D" LAST_NAME="" USERNAME="ctu_aa4849_jie" />
  <I EMAIL="[email protected]" FIRST_NAME="" INDIVIDUAL_KEY="11" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="rnopwakc" />
  <I EMAIL="[email protected]" FIRST_NAME="" INDIVIDUAL_KEY="12" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="TRIsd" />
  <I EMAIL="[email protected]" FIRST_NAME="" INDIVIDUAL_KEY="13" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="ll20373196" />
  <I EMAIL="[email protected]" FIRST_NAME="Sa" INDIVIDUAL_KEY="14" LANGUAGE_CODE="FRA" LAST_NAME="w" USERNAME="wsp" />
</I-collection>

Serialization.Deserialize does this:

public static T Deserialize<T>(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                throw new ArgumentNullException("xml");
            }


            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StringReader stream = new StringReader(xml))
            {
                try
                {
                    return (T)serializer.Deserialize(stream);
                }

                catch (Exception ex)
                { 
                    throw new InvalidOperationException("Failed to create object from xml string", ex);
                }
            }
        }

The problem is the object it returns is empty (I mean the itemsField List is empty). This deserialization should actually populate the list automatically using the data from the XML right ? or do I have to do it manually ?

3
  • 1
    Have you tried removing Form = System.Xml.Schema.XmlSchemaForm.Unqualified ? Checking now... Commented May 20, 2015 at 8:11
  • That was the problem. Thanks! Commented May 20, 2015 at 8:13
  • if in doubt, cheat: xsd my.xml followed by xsd my.xsd /c will generate my.cs Commented May 20, 2015 at 8:17

2 Answers 2

4

The Form is incorrect; try:

[System.Xml.Serialization.XmlElementAttribute("I", Order = 0)]
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"C:\temp\test.xml";
        static void Main(string[] args)
        {

            MyCollection myCollection = new MyCollection();

            XmlSerializer xs = new XmlSerializer(typeof(MyCollection.I_Collection));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            MyCollection.I_Collection newRoot = (MyCollection.I_Collection)xs.Deserialize(reader);

        }
    }
    public partial class MyCollection
    {
        [XmlRoot("I-collection")]
        public class I_Collection
        {
            [XmlElement("I")]
            public List<I> itemsField { get; set; }

        }

        [XmlRoot("I")]
        public partial class I
        {
            [XmlAttribute("EMAIL")]
            public string EMAILField { get; set; }

            [XmlAttribute("FIRST_NAME")]
            public string FIRST_NAMEField { get; set; }

            [XmlAttribute("INDIVIDUAL_KEY")]
            public string INDIVIDUAL_KEYField { get; set; }

            [XmlAttribute("LANGUAGE_CODE")]
            public string LANGUAGE_CODEField { get; set; }

            [XmlAttribute("LAST_NAME")]
            public string LAST_NAMEField { get; set; }

            [XmlAttribute("USERNAME")]
            public string USERNAMEField { get; set; }
        }
    }



}

Comments

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.