0

I try to deserialize a nested XML via generics (List<T>). Deserializing a not-nested XML is working, but the nested version is not.

XML:

<?xml version="1.0" encoding="utf-8"?>
<Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<User>
    <UserId>1</UserId>
    <LoginData>
        <LoginName>Albert</LoginName>
        <Password>albert</Password>
    </LoginData>
    <UserData>
        <FirstName>Albert</FirstName>
        <Surname>Einstein</Surname>
        <BirthDate>2879-03-14T00:00:00</BirthDate>
        <BirthPlace>German</BirthPlace>
        <AddressCity>Württemberg</AddressCity>
    </UserData>
</User>
</Users>

UserData.cs:

[Serializable]
public class MainData
{
    public int UserId { get; set; }
    public LoginData TestLoginData { get; set; }

    public UserData TestUserData { get; set; }
}

public class LoginData
{

    public string LoginName { get; set; }
    public string Password { get; set; }
}

public class UserData
{
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public DateTime BirthDate { get; set; }
    public string BirthPlace { get; set; }
    public string AddressCity { get; set; }
}

Deserializing:

public class XmlDataManager<T> : IDataManager<T> 
    where T : class
{
    public List<T> Load(string pathAndName)
    {
        List<T> result = null;

        StringReader stream = null;
        XmlTextReader reader = null;

        var xDocument = XDocument.Load(pathAndName);
        string xml = xDocument.ToString();

        XmlRootAttribute xRoot = new XmlRootAttribute();
        xRoot.ElementName = "Users";
        xRoot.IsNullable = true;

        try
        {
            stream = new StringReader(xml);
            reader = new XmlTextReader(stream);

            result = new XmlSerializer(typeof(List<T>), 
                     xRoot).Deserialize(reader) as List<T>;
         }
        catch (Exception ex)
        {
            Console.WriteLine(ex.GetBaseException());
        }
        return result;
    }

    // more code.....
  }
}

Part of User.cs:

[Serializable]
public class User : NotificationObject
{
    MainData data;

    public User()
    {
        data = new MainData();
    }

    //more code.....

    public override string ToString()
    {
        return UserId + LoginName + Password + FirstName + Surname + 
               BirthDate + BirthPlace + AddressCity;
    }

    //more code.....

    public int UserId
    {
        get { return data.UserId; }

        set
        {
            NotifyPropertyChanging("UserId");
            data.UserId = value;
            NotifyPropertyChanged("UserId");
        }
    }

    public string LoginName
    {
        get { return data.TestLoginData.LoginName; }

        set
        {
            NotifyPropertyChanging("LoginName");
            data.TestLoginData.LoginName = value;
            NotifyPropertyChanged("LoginName");
        }
    }

    ..... 

What I can't figure out is why importing the data of the XML is going oke:

var xDocument = XDocument.Load(pathAndName);
string xml = xDocument.ToString();

But deserializing is giving me null-values. What is going wrong?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Denis
  • 9
  • 1
  • 4
    Possible duplicate of [Is it possible to deserialize XML into List?](https://stackoverflow.com/questions/608110/is-it-possible-to-deserialize-xml-into-listt) – Alex Riabov Jun 24 '18 at 19:45
  • Your xml tag name and the class tagname is different try following : [XmlElement("UserData")]public UserData TestUserData { get; set; } – jdweng Jun 24 '18 at 19:47

1 Answers1

0

The thing is that your naming in class mismatches naming in xml. The following can help:

[Serializable]
[XmlType("User")]
public class MainData
{
    public int UserId { get; set; }

    [XmlElement("LoginData")]
    public LoginData TestLoginData { get; set; }

    [XmlElement("UserData")]
    public UserData TestUserData { get; set; }
}
Alex Riabov
  • 8,655
  • 5
  • 47
  • 48
  • Thank you all for trying to help me. I tried your solutions with the xml-tags (xmlelement), but unfortunately it is still not working: Result: - UserId gets the value 1 - The variable in the class LoginData and the variables in the class UserData are not showing up. It looks like if they not there. public User() { data = new MainData(); } This statement does show LoginData / UserData, but not the variables like LoginName, etc.. So eventually I get a null exception. Not surprisingly.. – Denis Jul 01 '18 at 15:49