简体   繁体   中英

How to structure the class for this XML?

How would I go about deserializing this XML into a class in 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>

I've done a little serializing and deserializing with XML before, but for some reason I'm having trouble finding the correct structure for the classes with this XML.

So far this is about all I have and it doesn't work. Could anyone structure these classes to work with the above 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;
}

It's throwing this error.

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.

Got it with these classes. Please be very careful as some changes have been case sensitive !

// 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;
}

Loaded data with

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);
    }           
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM