Why deserialize XML into Object return null value?
Asked Answered
C

2

0

I have a XML string like that:

<?xml version="1.0" ?>
<result>
<vmeet_id>7121</vmeet_id>
<username>MT_Hue_QuangBinh_QuangTri</username>
<email></email>
<begin_date>2010-04-21 08:53</begin_date>
<expiry_date>2010-12-21 00:00</expiry_date>
<point></point>
<info>OK</info>
</result>

I want to deserialize it into an object, so I created this class:

[Serializable] 
[XmlRoot(ElementName = "result", IsNullable = false)]
public class UserInfo
{
    [XmlAttribute("vmeet_id")]
    public int UserID { get; set; }
    [XmlAttribute("username")]
    public string Username { get; set; } 
    [XmlAttribute("email")]
    public string Email { get; set; }
    [XmlAttribute("begin_date")]
    public DateTime BeginDate { get; set; }
    [XmlAttribute("expiry_date")]
    public DateTime ExpiryDate { get; set; }
    [XmlAttribute("point")]
    public string Point { get; set; }
    [XmlAttribute("info")]
    public string Info { get; set; }
}

and then use this code to deserialize:

var deserializer = new XmlSerializer(typeof(UserInfo));
        using (var stream = new StringReader(result))
        {
            UserInfo userInfo = (UserInfo)deserializer.Deserialize(stream);
            return userInfo;
        }

return value was not null, but all its properties was null value:

<result vmeet_id="0" begin_date="0001-01-01T00:00:00" expiry_date="0001-01-01T00:00:00"/>

what is wrong here? Did I forgot something?

Thank you.

Carlist answered 23/11, 2010 at 2:32 Comment(1)
As a side note: if you have a chunk of XML; you could use the xsd.exe command line tool to generate a XML schema and in a second step a C# class from that XML that will be able to deserialize that XML into a C# class. Just a thought... instead of a lot of hours of hand-coding - just called xsd.exe twice - and you're done!Philipphilipa
P
8

In your XML, all your 'vmeet' 'begin_date' are all elements, but in your UserInfo Class, you declare them as XMLAttribute. Try changing them to XMLElement should help.

Paley answered 23/11, 2010 at 2:35 Comment(2)
another problem, when I deserialize it, the datetime value cannot be deserialize properly (it seems that lack of schema), end returns value: The string '2010-04-21 08:53' is not a valid AllXsd value. Can you suggest a workaround?Carlist
I am not sure which type of Datetime format MS is using in serialization, but most probably it should adhere to the standard XSD Datetime format: w3schools.com/schema/schema_dtypes_date.aspPaley
L
0

Use XmlDocument and Json to easily resolve result.

        public static T XmlToModel<T>(string xml)
        {

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            string jsonText = JsonConvert.SerializeXmlNode(doc);

            T result = JsonConvert.DeserializeObject<T>(jsonText);

            return result;
        }
Leasia answered 30/9, 2020 at 9:32 Comment(3)
Why deserialize string to XML document, then serializing to JSON string, then finally deserialize the JSON to object?Unfreeze
Shortcut, but valid, had a difficult scenario where this was the easiest way tbhGlendaglenden
Looks like a good idea, the problem is that this creates new problems, for example deserializing bool (can't deserialize 0 or 1 as boolean).Evangelin

© 2022 - 2024 — McMap. All rights reserved.