日期:2014-05-20 浏览次数:20444 次
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; } } }
------解决方案--------------------
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();