C#讀取XML文件的基類實現

来源:http://www.cnblogs.com/DreamOfLife/archive/2016/05/24/5525296.html
-Advertisement-
Play Games

剛到新單位,學習他們的源代碼,代碼里讀寫系統配置文件的XML代碼比較老套,直接寫在一個系統配置類里,沒有進行類的拆分,造成類很龐大,同時,操作XML的讀寫操作都是使用SetAttribute和node.Attribute(name)方法,因此,想到結合之前所做的XML操作,完成了一個能夠讀取XML文 ...


剛到新單位,學習他們的源代碼,代碼里讀寫系統配置文件的XML代碼比較老套,直接寫在一個系統配置類里,沒有進行類的拆分,造成類很龐大,同時,操作XML的讀寫操作都是使用SetAttribute和node.Attribute(name)方法,因此,想到結合之前所做的XML操作,完成了一個能夠讀取XML文件的基類,便於以後的使用。

PS:即使再老套的代碼,目前也不敢進行優化,一是水平不行,二是不敢。

使用靜態擴展類,擴展了幾個經常使用的類型,能夠方便數據的讀寫。

操作XML的類,可以直接繼承BaseLinqXmlFileInfo,只需要設置protected類型的變數mPathName,mFileName,然後重寫抽象方法即可實現XML文檔的讀取操作。

PS:能力有限,有不對之處請指正,謝謝。

  1 using System;
  2 using System.IO;
  3 using System.Xml.Linq;
  4 
  5 namespace Common
  6 {
  7     public abstract class BaseLinqXmlFileInfo
  8     {
  9         protected string mPathName;
 10 
 11         protected string mFileName;
 12 
 13         protected virtual string PathName
 14         {
 15             get { return mPathName; }
 16             set { mPathName = value; }
 17         }
 18 
 19         protected virtual string FileName
 20         {
 21             get { return mFileName; }
 22             set { mFileName = value; }
 23         }
 24 
 25         protected virtual string FilePathName
 26         {
 27             get
 28             {
 29                 return Path.Combine(PathName, FileName);
 30             }
 31         }
 32 
 33         public virtual bool Load()
 34         {
 35             bool result = false;
 36             try
 37             {
 38                 string filePathName = this.FilePathName;
 39                 if (File.Exists(filePathName))
 40                 {
 41                     this.LoadDocument(filePathName);
 42                     result = true;
 43                 }
 44             }
 45             catch(Exception ex)
 46             {
 47                 //異常信息輸出
 48             }
 49             return result;
 50         }
 51 
 52         public virtual bool Save()
 53         {
 54             bool result = false;
 55             try
 56             {
 57 
 58                 string pathName = this.PathName;
 59                 if (!Directory.Exists(pathName))
 60                 {
 61                     Directory.CreateDirectory(PathName);
 62                 }
 63                 string tempFileName = "~" + FileName;
 64                 string tempfilePathName = Path.Combine(pathName, tempFileName);
 65                 this.SaveDocument(tempfilePathName);
 66                 string filePathName = Path.Combine(PathName, FileName);
 67                 if (File.Exists(filePathName))
 68                 {
 69                     FileAttributes att = File.GetAttributes(filePathName);
 70                     if((att & FileAttributes.ReadOnly)== FileAttributes.ReadOnly)
 71                     {
 72                         File.SetAttributes(filePathName, att & ~FileAttributes.ReadOnly);
 73                     }
 74                 }
 75                 File.Copy(tempfilePathName, filePathName, true);
 76                 if (File.Exists(tempfilePathName))
 77                 {
 78                     File.Delete(tempfilePathName);
 79                 }
 80                 result = true;
 81             }
 82             catch (Exception ex)
 83             {
 84                 //異常信息輸出
 85             }
 86             return result;
 87         }
 88 
 89         private void LoadDocument(string fileName)
 90         {
 91             try
 92             {
 93                 XDocument doc = XDocument.Load(fileName);
 94                 this.ReadXml(doc);
 95             }
 96             catch(Exception ex)
 97             {
 98                 //異常信息輸出
 99             }
100         }
101 
102         private void SaveDocument(string fileName)
103         {
104             try
105             {
106                 XDocument doc = new XDocument();
107                 this.WriteXml(doc);
108                 doc.Save(fileName);
109             }
110             catch(Exception ex)
111             {
112                 //異常信息輸出
113             }
114         }
115 
116         private void ReadXml(XDocument doc)
117         {
118             XElement root = doc.Root;
119             this.ReadXml(root);
120         }
121 
122         private void WriteXml(XDocument doc)
123         {
124             XElement root = new XElement("root");
125             doc.Add(root);
126             this.WriteXml(root);
127         }
128 
129         protected abstract void ReadXml(XElement node);
130 
131         protected abstract void WriteXml(XElement node);
132     }
133 
134     public static class XMLSerializeExtensionClass
135     {
136         public static void Write(this XElement node,string name,string value)
137         {
138             node.SetAttributeValue(name, value);
139         }
140 
141         public static string ReadString(this XElement node, string name, string defaultValue)
142         {
143             XAttribute att = node.Attribute(name);
144             return att != null ? att.Value : defaultValue;
145         }
146 
147         public static void Write(this XElement node,string name,long value)
148         {
149             node.SetAttributeValue(name, value);
150         }
151 
152         public static long ReadLong(this XElement node,string name,long defaultValue)
153         {
154             XAttribute att = node.Attribute(name);
155             return att != null ? Convert.ToInt64(att.Value) : defaultValue;
156         }
157 
158         public static void Write(this XElement node,string name,decimal value)
159         {
160             node.SetAttributeValue(name, value);
161         }
162 
163         public static decimal ReadDecimal(this XElement node,string name,decimal defaultValue)
164         {
165             XAttribute att = node.Attribute(name);
166             return att != null ? Convert.ToDecimal(att.Value) : defaultValue;
167         }
168 
169         public static void Write(this XElement node ,string name,DateTime value)
170         {
171             node.SetAttributeValue(name, value);
172         }
173 
174         public static DateTime ReadDateTime(this XElement node,string name,DateTime defaultValue)
175         {
176             XAttribute att = node.Attribute(name);
177             return att != null ? Convert.ToDateTime(att.Value) : defaultValue;
178         }
179 
180         public static void Write(this XElement node,string name,int value)
181         {
182             node.SetAttributeValue(name, value);
183         }
184 
185         public static int ReadInt(this XElement node,string name,int defaultValue)
186         {
187             XAttribute att = node.Attribute(name);
188             return att != null ? Convert.ToInt32(att.Value) : defaultValue;
189         }
190 
191         public static void Write(this XElement node, string name,bool value)
192         {
193             node.SetAttributeValue(name, value);
194         }
195 
196         public static bool ReadBoolean(this XElement node, string name, bool defaultValue)
197         {
198             XAttribute att = node.Attribute(name);
199             return att != null ? Convert.ToBoolean(att.Value) : defaultValue;
200         }
201     }
202 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 摘要: Linux系統,中文顯示亂碼 XShell是一個強大的安全終端模擬軟體,它支持SSH1, SSH2及 Microsoft Windows平臺的Telnet NetSarang Xshell 4 Build 0120協議。使用Xshell可以快速方便的管理Linux主機。 我們在使用時,可能會 ...
  • (1)遠程連接的時候在本地用戶名添加功能變數名稱 現象:在Windows2008R2配置成域控制器前,還沒有安裝AD,管理員Admistrator以及新創建的用戶(創建的時候將該用戶加入遠程桌面管理組),發現可以直接輸入遠程主機的IP和本地用戶名、密碼能直接登錄到遠程主機。 操作:安裝在Windows200 ...
  • 裡面功能變數名稱重覆: 在vhosts下多個虛擬機配置文件,都是基於功能變數名稱配置的,其中兩個配置文件,都起了localhost ,所以會報錯!!!! 多個功能變數名稱可以指向同一個目錄,但同一個功能變數名稱不可一指向多個目錄!!!!!! ...
  • 0.檢查配置 1. VMWare上運行的Ubuntu,並不能支持真實的GPU(除了特定版本的VMWare和特定的GPU,要求條件嚴格,所以我在VMWare上搭建好了Caffe環境後,又重新在Windows 7 64bit系統上安裝了Ubuntu 14.04 64bit系統,鏈接 "在此" ,以此來搭 ...
  • 註釋 註釋 註釋毫無疑問是讓別人以最快速度瞭解你代碼的最快途徑,但寫註釋的目的絕不僅僅是"解釋代碼做了什麼",更重要的儘量幫助代碼閱讀者對代碼瞭解的和作者一樣多。 當你寫代碼時,你腦海裡會有很多有價值的信息,但當其他人讀你代碼時,這些信息已經丟失,他們所見到的只是眼前代碼。 註釋約定 註釋約定 如果 ...
  • Insus.NET開發這樣多網站,客戶一直沒有這個要求。不過,現在有客戶有這樣的要求了。線上用戶訪問人數,也就是說,要為網站寫一個計數器,計數器的初始值為0,網站一開始運行時(Application_Start),就開始統計,當有用戶訪問時(Session_Start)計數器加1,當用戶訪問離開時( ...
  • 三大範式一直沒有記住,看了這個有了理解!挺好的記著,以後忘了,可以再看看! 為了建立冗餘較小、結構合理的資料庫,設計資料庫時必須遵循一定的規則。在關係型資料庫中這種規則就稱為範式。範式是符合某一種設計要求的總結。要想設計一個結構合理的關係型資料庫,必須滿足一定的範式。 在實際開發中最為常見的設計範式 ...
  • 環境:Vs2103(TFS2013) 目的:去掉別人項目里的TFS控制,因為每次打開時會有提示信息 解決方法: 1.刪除隱藏的.$tf文件夾,搜索*.vssscc和*.vspscc這兩個尾碼的文件,刪除找到的文件. 2.使用文本編輯器打開*.sln文件,找到 GlobalSection(TeamFo ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...