日期:2014-05-20  浏览次数:20444 次

xml反序列化,请高人帮忙啊
我想要下面这个xml文件的反序列化类,不会写啊,求大侠帮忙
<?xml version="1.0" encoding="utf-8"?>
<cameras>
  <camera id="0" name="高清" url="rtsp://192.168.16.84/media/video1" type="RTSP" NATTraserval="0" user="Administrator" pwd="" discribe="" enable="1" />
  <camera id="1" name="热成像" url="http://192.168.16.200" type="ITG" NATTraserval="0" user="Administrator" pwd="" discribe="" enable="1" />
  <camera id="2" name="跑道南" url="rtsp://192.168.16.84/media/video2" type="RTSP" NATTraserval="1:10" user="Administrator" pwd="" discribe="" enable="1" />
  <camera id="3" name="跑道北" url="rtsp://192.168.16.84/media/video2" type="RTSP" NATTraserval="1:10" user="Administrator" pwd="" discribe="" enable="0" /> 
</cameras>


这是列表,不知道该怎么写

------解决方案--------------------
C# code

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strPath = "D:\\xx.xml";
        /**
        //序列化
        UserInfo user = new UserInfo("Ranen",30);
        XmlSerializer xml = new XmlSerializer(typeof(UserInfo));
        FileStream fs = File.Create(strPath);
        xml.Serialize(fs, user);
        fs.Close();
         * */

        //反序列化
        XmlSerializer xml = new XmlSerializer(typeof(UserInfo));
        FileStream fs = File.OpenRead(strPath);
        UserInfo user = xml.Deserialize(fs) as UserInfo;
    }
}
[Serializable]
public class UserInfo
{
    public UserInfo() { }
    public UserInfo(string userName ,int age)
    {
        UserName = userName;
        Age = age;
    }
    private string _UserName;
    [XmlAttribute("UserName")]
    public string UserName
    {
        get { return _UserName; }
        set { _UserName = value; }
    }

    private int _Age;
    [XmlAttribute("Age")]
    public int Age
    {
        get { return _Age; }
        set { _Age = value; }
    }
}

------解决方案--------------------
C# code
    public class cameras
    {
        [XmlElement("camera")]
        public camera[] camera;
    }
    public class camera
    {
        [XmlAttribute("id")]
        public string id { get; set; }
        [XmlAttribute("name")]
        public string name { get; set; }
        [XmlAttribute("url")]
        public string url { get; set; }
        [XmlAttribute("type")]
        public string type { get; set; }
        [XmlAttribute("NATTraserval")]
        public string NATTraserval { get; set; }
        [XmlAttribute("user")]
        public string user { get; set; }
        [XmlAttribute("pwd")]
        public string pwd { get; set; }
        [XmlAttribute("discribe")]
        public string discribe { get; set; }
        [XmlAttribute("enable")]
        public string enable { get; set; }
    }

           cameras c = new cameras();
            c.camera = new camera[2] { 
                new camera() { id = "1", name = "csdn", url = "www.csdn.com", type = "csdn", NATTraserval = "csdn", user = "csdnuser", pwd = "csdnpwd", discribe = "csdndiscribe", enable = "1" },
                new camera() { id = "2", name = "cnblogs", url = "www.cnblogs.com", type = "cnblogs", NATTraserval = "cnblogs", user = "cnblogsuser", pwd = "cnblogspwd", discribe = "cnblogsdiscribe", enable = "2" }};
            XmlSerializer xml = new XmlSerializer(typeof(cameras));
            FileStream fs = new FileStream(@"E:\xml.xml", FileMode.Create, FileAccess.Write);
            xml.Serialize(fs, c);
            fs.Close();
            fs.Dispose();