Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
<root>
    <posts>
        <post>
            <id>7</id>
            <USERNAME>sohaib</USERNAME>
            <TITLE>help</TITLE>
            <USERID>1</USERID>
        </post>
        <post>
            <id>7</id>
            <USERNAME>sohaib</USERNAME>
            <TITLE>help</TITLE>
            <USERID>1</USERID>
        </post>
    </posts>
    <comments>
        <comment>
            <COMMENTID>2</COMMENTID>
            <COMMENT_TEXT>help</COMMENT_TEXT>
        </comment>
        <comment>
            <COMMENTID>2</COMMENTID>
            <COMMENT_TEXT>help</COMMENT_TEXT>
        </comment>
        <comment>
            <COMMENTID>2</COMMENTID>
            <COMMENT_TEXT>help</COMMENT_TEXT>
        </comment>
    </comments>
</root>



and my C# code to deserialize this above xml file is

C#
XmlSerializer xsserializer = new XmlSerializer(typeof(root));
FileStream reader = new FileStream(&quot;abc.xml&quot;,FileMode.Open);
object obj = xsserializer.Deserialize(reader);
root timeline = (root) obj;


Where my Root class looks like

C#
[Serializable, XmlRoot(&quot;root&quot;), XmlType(&quot;root&quot;)]
public class root
{
    [XmlElement(&quot;Posts&quot;)]
    public List&lt;Post&gt;  Posts =new List&lt;Post&gt;();
    [XmlElement(&quot;Comments&quot;)]
    public List&lt;Comment&gt; Comments = new List&lt;Comment&gt;();

}
public class Post
{
    [XmlElement(&quot;TITLE&quot;)]
    public string TITLE;
    [XmlElement(&quot;USERID&quot;)]
    public long USERID;
    [XmlElement(&quot;USERNAME&quot;)]
    public string USERNAME;
    [XmlElement(&quot;id&quot;)]
    public long id;
}

public class Comment
{
    [XmlElement(&quot;COMMENTID&quot;)] public long COMMENTID;
    [XmlElement(&quot;COMMENT_TEXT&quot;)] public string COMMENT_TEXT;

}


I'm facing no exception no error but still it gives 0 items in posts and 0 in comments.
Can you please help me figure out where I'm going wrong.

I appreciate your response.

Thanks Sohaib
Posted
Updated 11-Oct-15 10:29am
v2

1 solution

2 notes:
1) c# is case sensitive, which means that post is not equal to Post
2) List(of comments) and List(of posts) is declared in wrong way

Change its declaration to:
C#
[Serializable, System.Xml.Serialization.XmlRoot("root"), System.Xml.Serialization.XmlType("root")]
public class root
{
    //instead of: [System.Xml.Serialization.XmlElement("Posts")], use:
    [System.Xml.Serialization.XmlArray("posts"), System.Xml.Serialization.XmlArrayItem("post", typeof(Post))]
    public List<Post>  Posts =new List<Post>();
    //[System.Xml.Serialization.XmlElement("Comments")]
    [System.Xml.Serialization.XmlArray("comments"), System.Xml.Serialization.XmlArrayItem("comment", typeof(Comment))]
    public List<Comment> Comments = new List<Comment>();

}


For further information, please see:
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[^]
 
Share this answer
 
Comments
Sohaib Javed 11-Oct-15 17:08pm    
Thanks for your response. I'm giving it a try.
Sohaib Javed 11-Oct-15 18:15pm    
Hey thats great. Its working. Could you please tell me how to solve this exception while deserializing?

"The string '9/30/2015 12:59:37 PM' is not a valid AllXsd value"

whereas I'm receiving it in a string. This is response I'm getting from web service. So I can't modify xml.
Maciej Los 12-Oct-15 1:53am    
DateTime convertion from string depends on localization. Please, follow this: Globalizing and Localizing .NET Framework Applications. The best way to workaround it is to use DateTime.TryParse method within IFormatProvider as ClutureInfo Class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900