XML反序列化不返回任何内容 [英] XML deserialization returns nothing

查看:125
本文介绍了XML反序列化不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   root  >  
< 帖子 >
< post >
< id > 7 < / id >
< USERNAME > sohaib < / USERNAME >
< TITLE > help < / TITLE >
< USERID > 1 < / USERID >
< / post >
< < span class =code-leadattribute> post >
< id > 7 < / id >
< USERNAME > sohaib < / USERNAME >
< TITLE > help < / TITLE >
< USERID > 1 < / USERID >
< / post >
< / posts >
< 评论 >
< 评论 >
< 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 >







和我的C#代码来反序列化thi在xml文件上面是



 XmlSerializer xsserializer =  new  XmlSerializer(< span class =code-keyword> typeof (root)); 
FileStream reader = new FileStream(& quot; abc.xml& quot;,FileMode.Open);
object obj = xsserializer.Deserialize(reader);
root timeline =(root)obj;





我的Root类看起来像



< pre lang =c#> [Serializable,XmlRoot(& quot; root& quot;),XmlType(& quot; root& quot;)]
public class root
{
[XmlElement(& quot; Posts& quot;)]
public List& lt; Post& gt;帖子= 列表& lt; Post& gt;();
[XmlElement(& quot; Comments& quot;)]
public List& lt; Comment& gt;评论= 列表& lt; Comment& gt;();

}
public class 发​​布
{
[XmlElement(& quot; TITLE& quot;)]
public string 标题;
[XmlElement(& quot; USERID& quot;)]
public long USERID;
[XmlElement(& quot; USERNAME& quot;)]
public string USERNAME;
[XmlElement(& quot; id& quot;)]
public long id;
}

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

}





我面临无异常没有错误但仍然在帖子中提供0项和0在评论中。

你能帮我弄清楚我哪里出错了。



我很感激你的回复。



谢谢Sohaib

解决方案

2注意事项:

1)c#是区分大小写的,意味着发布不等于发布

2)列表(评论)列表(帖子)以错误方式声明



将其声明更改为:

 [Serializable,System.Xml.Serialization.XmlRoot( < span class =code-string> root),System.Xml.Serialization.XmlType(  root)] 
public class root
{
/ / 而不是:[System.Xml.Serialization.XmlElement(Posts)],使用:
[System.Xml.Serialization。 XmlArray( posts),System.Xml.Serialization.XmlArrayItem( post typeof (Post))]
< span class =code-keyword> public List< Post>帖子= 列表< Post>();
// [System.Xml.Serialization.XmlElement(Comments)]
[System.Xml.Serialization.XmlArray( comments),System.Xml.Serialization。 XmlArrayItem( comment typeof (评论))]
public 列表<评论>评论= 列表<评论>();

}





如需了解更多信息,请参阅:

XML序列化和反序列化:第1部分 [ ^ ]

XML序列化和反序列化:第2部分 [ ^ ]

自定义类集合序列化和反序列化的完整示例 [ ^ ]


<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

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

[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

解决方案

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:

[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[^]


这篇关于XML反序列化不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆