0

我将如何将这个 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.
4

1 回答 1

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);
    }           
}
于 2012-09-01T19:57:40.830 回答