日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

XML文件与实体类的互相转换

發布時間:2024/4/17 asp.net 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 XML文件与实体类的互相转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

 1. 通常程序的配置信息都保存在程序或者網站的專門的配置文件中(App.config/web.config)。但是現在為了演示XML序列化和反序列化,將配置信息保存在一個XML文件(config.xml)中,通過反序列化將配置信息讀取出來保存到一個單獨的類(Config.cs)中。這樣如果需要用到配置信息,沒必要每次都讀寫XML文件,只需要調用Config這個類就可以獲取對應節點的信息。

  config.xml:

<?xml version="1.0" encoding="utf-8"?> <Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" IsAuto="true">
<Description>定時掃描數據庫,通過客戶號和業務號讀取客戶信息</Description>
<CustomerInfos><CustomerInfo><CustomerId>0013</CustomerId><BusinessId>03</BusinessId></CustomerInfo><CustomerInfo><CustomerId>0022</CustomerId><BusinessId>02</BusinessId></CustomerInfo></CustomerInfos>
<ScanConfigs><BeginTime>22:00:00</BeginTime><EndTimme>23:00:00</EndTimme></ScanConfigs>
</Config>

  2. 為了將上面這個XML轉換為想要的實體類對象,方便在程序里面讀取節點數據,需要創建一個相對應的實體類,在實體類中用[XmlRoot][XmlElement][XmlAttribute]等屬性標識。

  Config.cs:

  //XmlRoot表明這個類對應的是XML文件中的根節點
  [XmlRoot(ElementName="Config")]public class Config{
     //XmlElement表明這個字段對應的是XML文件中當前父節點下面的一個子節點
     //ElementName就是XML里面顯示的當前節點名稱
     //類中的字段名稱與對應的XML節點的名稱可以不同(比如在這里類Config中的屬性ClientDescription對應XML文件中根節點Config下面的子節點Description)[XmlElement(ElementName = "Description")]public string ClientDescription { get; set; }
     //XmlAttribute表明這個字段是XML文件中當前節點的一個屬性[XmlAttribute(AttributeName="IsAuto")]public string IsAuto { get; set; }[XmlElement(ElementName = "CustomerInfos")]public CustomerInfos CustomerInfos{get;set;}[XmlElement(ElementName = "ScanConfigs")]public ScanConfigs ScanConfigs{get;set;}}public class CustomerInfos{[XmlElement(ElementName = "CustomerInfo")]public CustomerInfo[] cs{get;set;}}public class CustomerInfo{[XmlElement(ElementName = "CustomerId")]public string CustomerId { get; set; }[XmlElement(ElementName = "BusinessId")]public string BusinessId { get; set; }}public class ScanConfigs{[XmlElement(ElementName = "BeginTime")]public string BeginTime { get; set; }[XmlElement(ElementName = "EndTimme")]public string EndTimme { get; set; }}

  3. 下面的代碼調用.net的XmlSerializer類的方法進行XML的反序列化

  public class XmlUtil{//反序列化
     //接收2個參數:xmlFilePath(需要反序列化的XML文件的絕對路徑),type(反序列化XML為哪種對象類型)public static object DeserializeFromXml(string xmlFilePath, Type type){object result = null;if (File.Exists(xmlFilePath)){using (StreamReader reader = new StreamReader(xmlFilePath)){XmlSerializer xs = new XmlSerializer(type);result = xs.Deserialize(reader);}}return result;}}

  4. 反序列化

   string xmlPath = "d:\\config.xml";Config c = XmlUtil.DeserializeFromXml(xmlPath, typeof(Config)) as Config;

二. 序列化

  1. 反過來的,也可以將Config類的一個對象序列化為XML文件.下面的代碼通過調用.net的XmlSerializer類的方法將對象序列化為XML文件

  public class XmlUtil{//序列化
     //接收4個參數:srcObject(對象的實例),type(對象類型),xmlFilePath(序列化之后的xml文件的絕對路徑),xmlRootName(xml文件中根節點名稱)
     //當需要將多個對象實例序列化到同一個XML文件中的時候,xmlRootName就是所有對象共同的根節點名稱,如果不指定,.net會默認給一個名稱(ArrayOf+實體類名稱)public static void SerializeToXml(object srcObject, Type type,string xmlFilePath, string xmlRootName){if (srcObject != null && !string.IsNullOrEmpty(xmlFilePath)){type = type != null ? type : srcObject.GetType();using(StreamWriter sw=new StreamWriter(xmlFilePath)){XmlSerializer xs = string.IsNullOrEmpty(xmlRootName) ?new XmlSerializer(type) :new XmlSerializer(type, new XmlRootAttribute(xmlRootName));xs.Serialize(sw, srcObject);}}}}

  2. 序列化

       Config config = new Config();config.ClientDescribe = "定時掃描數據庫,通過客戶號和業務號讀取客戶信息.";config.IsAuto = "true";CustomerInfo ci1 = new CustomerInfo();ci1.CustomerId = "0013";ci1.BusinessId = "03";CustomerInfo ci2 = new CustomerInfo();ci2.CustomerId = "0022";ci2.BusinessId = "02";CustomerInfos cis = new CustomerInfos();cis.cs = new CustomerInfo[] { ci1, ci2 };config.CustomerInfos = cis;ScanConfigs sc = new ScanConfigs();sc.BeginTime = "22:00:00";sc.EndTimme = "23:00:00";config.ScanConfigs = sc;XmlUtil.SerializeToXml(config, config.GetType(), "d:\\config.xml", null);

轉載于:https://www.cnblogs.com/sjqq/p/7725125.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的XML文件与实体类的互相转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。