如何将此XML反序列化为C#中的类?
<?xml version="1.0" encoding="UTF-8"?>
<root>
<infos>
</infos>
<users>
<user userID="1" userRole="1" channelID="0"><![CDATA[Test_User]]></user>
<user userID="2" userRole="0" channelID="0"><![CDATA[Test_User_2]]></user>
</users>
<messages>
<message id="349992" dateTime="Sat, 01 Sep 2012 18:29:48 +0000" userID="1" userRole="1" channelID="0">
<username><![CDATA[Test_User]]></username>
<text><![CDATA[Test Message]]></text>
</message>
<message id="349993" dateTime="Sat, 01 Sep 2012 18:29:50 +0000" userID="2" userRole="0" channelID="0">
<username><![CDATA[Test_User_2]]></username>
<text><![CDATA[Test Message 2]]></text>
</message>
</messages>
</root>
之前我已经使用XML进行了一些序列化和反序列化,但由于某种原因,我无法使用此XML为类找到正确的结构。
到目前为止,这是关于我所拥有的一切,但它不起作用。 任何人都可以构造这些类来使用上面的XML吗?
[Serializable]
public class User
{
[XmlAttribute("userID")]
public object UserId;
[XmlAttribute("userRole")]
public object UserRole;
[XmlAttribute("channelID")]
public object ChannelId;
}
[Serializable]
public class Message
{
[XmlAttribute("id")]
public object Id;
[XmlAttribute("dateTime")]
public object DateTime;
[XmlAttribute("userID")]
public object UserId;
[XmlAttribute("userRole")]
public object UserRole;
[XmlAttribute("channelID")]
public object ChannelId;
[XmlElement("username")]
public object Username;
[XmlElement("text")]
public object Text;
}
[Serializable, XmlRoot("root")]
public class Root
{
[XmlElement("infos")]
public object Infos;
[XmlElement("users")]
public List<User> Users;
[XmlElement("messages")]
public List<Message> Messages;
}
它抛出了这个错误。
System.InvalidOperationException was unhandled by user code
Message=There was an error reflecting type 'ConsoleApplication.Client.Root'.
Message=There was an error reflecting field 'Users'.
Message=There was an error reflecting type 'ConsoleApplication.Client.User'.
Message=There was an error reflecting field 'UserId'.
Message=Cannot serialize member 'UserId' of type System.Object. XmlAttribute/XmlText cannot be used to encode complex types.
答案 0 :(得分:0)
了解这些课程。 请注意,因为某些更改区分大小写!
// only one xml root element is allowed!
[Serializable]
public class User
{
public User()
{ }
[XmlAttribute("userID")]
public string UserId;
[XmlAttribute("userRole")]
public string UserRole;
[XmlAttribute("channelID")]
public string ChannelId;
}
[Serializable]
public class Message
{
public Message()
{ }
[XmlAttribute("id")]
public string Id;
[XmlAttribute("dateTime")]
public string DateTime;
[XmlAttribute("userID")]
public string UserId;
[XmlAttribute("userRole")]
public string UserRole;
[XmlAttribute("channelID")]
public string ChannelId;
[XmlElement("username")]
public string Username;
[XmlElement("text")]
public string Text;
}
[Serializable, XmlRoot("root")]
public class Root
{
public Root()
{ }
[XmlElement("infos")]
public object Infos;
[XmlArray]
[XmlArrayItem(ElementName = "user", Type=typeof(User))]
public User[] users;
// define what type message is - XmlArray
// each item within is of c# type Message but within xml file it's message
[XmlArray]
[XmlArrayItem(ElementName = "message", Type = typeof(Message))]
public Message[] messages;
}
使用
加载数据private void frmXmlDeserialize_Load(object sender, EventArgs e)
{
try
{
XmlSerializer xs = new XmlSerializer(typeof(Root));
StreamReader sr = new StreamReader("import.xml");
Root r = (Root)xs.Deserialize(sr);
sr.Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}