




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、XMLHelper(C#版)(更新)默認分類 2008-10-07 15:39:59 閱讀1217 評論12 字號:大中小 XMLHelper.cs文件using System;using System.Data;using System.Configuration;using System.Linq;using System.Xml.Linq;using System.Xml;/ <summary>/ FileName: XMLHelper/ Author: ziyou/ Create Date: 2008-9-28/ Email: ysli_195/ Version: 1.0.
2、0.3/ Rewrite: 2009-2-3/ </summary>public class XMLHelper public XMLHelper() / / TODO: Add constructor logic here /
3、; #region private AppendChild private static void AppendChild(XmlDocument xDoc, XmlNode parentNode, params XmlParameter paras) foreach (XmlParameter xpar in paras)
4、60; XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xpar.Name, null); string ns = xpar.NamespaceOfPrefix = null ? "" : newNode.GetNamespaceOfPrefix(xp
5、ar.NamespaceOfPrefix); foreach (AttributeParameter attp in xpar.Attributes) X
6、mlNode attr = xDoc.CreateNode(XmlNodeType.Attribute, attp.Name, ns = "" ? null : ns); attr.Value = attp.Value;
7、 newNode.Attributes.SetNamedItem(attr); newNode.InnerText = xpar.InnerText; parentNode.AppendChild(ne
8、wNode); #endregion #region private AddEveryNode private static void AddEveryNode(XmlDocument xDoc, XmlNode parentNode, params XmlParameter paras)
9、160; XmlNodeList nlst = xDoc.DocumentElement.ChildNodes; foreach (XmlNode xns in nlst) if (xns.Name = parentNode.Name) &
10、#160; AppendChild(xDoc, xns, paras);
11、0; else foreach (XmlNode xn in xns)
12、; if (xn.Name = parentNode.Name)
13、60; AppendChild(xDoc, xn, paras);
14、160; #endregion #region xmlDoc / <summary> / 創建一個XmlDocument對象
15、 / </summary> / <param name="PathOrString">文件名稱或XML字符串</param> public static XmlDocument xmlDoc(string PathOrString) try
16、 XmlDocument xDoc = new XmlDocument(); if (System.IO.File.Exists(PathOrString)
17、; xDoc.Load(PathOrString); else
18、60; xDoc.LoadXml(PathOrString); return xDoc;
19、160; catch return null; #endregion #region CreateXMLFile&
20、#160; / <summary> / 創建一個XML文檔 / </summary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="rootName">根結點名稱</param> / <param
21、 name="elemName">元素節點名稱</param> / <param name="paras">XML參數</param> public static void CreateXMLFile(string fileFullName, string rootName, string elemName, params XmlParameter paras)
22、0; XmlDocument xDoc = new XmlDocument(); XmlNode xn; xn = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null); xDoc.AppendChild(xn);
23、160; XmlNode root = xDoc.CreateElement(rootName); xDoc.AppendChild(root); XmlNode ln = xDoc.CreateNode(XmlNodeType.Element, elemName, null); App
24、endChild(xDoc, ln, paras); root.AppendChild(ln); try xDoc.Save(fileFullName);
25、 catch throw; / <summary> / 創建一個
26、XML文檔 / </summary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="rootName">根結點名稱</param> / <param name="elemp">元素節點對象</param> &
27、#160; / <param name="paras">XML參數</param> public static void CreateXMLFile(string fileFullName, string rootName, XmlParameter elemp, params XmlParameter paras) XmlDocument xDoc = new XmlDocument();
28、160; XmlNode xn; xn = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null); xDoc.AppendChild(xn); XmlNode root = xDoc.Creat
29、eElement(rootName); xDoc.AppendChild(root); XmlNode ln = xDoc.CreateNode(XmlNodeType.Element, elemp.Name, null); string ns = elemp.NamespaceOfPrefix = null ? "" : l
30、n.GetNamespaceOfPrefix(elemp.NamespaceOfPrefix); foreach (AttributeParameter ap in elemp.Attributes) XmlNode elemAtt = xDoc.CreateNode(XmlNodeType.At
31、tribute, ap.Name, ns = "" ? null : ns); elemAtt.Value = ap.Value; ln.Attributes.SetNamedItem(elemAtt); &
32、#160; AppendChild(xDoc, ln, paras); root.AppendChild(ln); try xDoc.Save(fileFullNam
33、e); catch throw; / <sum
34、mary> / 創建一個XML文檔 / </summary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="rootp">根結點對象</param> / <param name="elemName">
35、元素節點名稱</param> / <param name="paras">XML參數</param> public static void CreateXMLFile(string fileFullName, XmlParameter rootp, string elemName, params XmlParameter paras) XmlDoc
36、ument xDoc = new XmlDocument(); XmlNode xn; xn = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null); xDoc.AppendChild(xn);
37、; XmlNode root = xDoc.CreateElement(rootp.Name); string ns = rootp.NamespaceOfPrefix = null ? "" : root.GetNamespaceOfPrefix(rootp.NamespaceOfPrefix); foreach (AttributeParameter ap in rootp.Attribute
38、s) XmlNode rootAtt = xDoc.CreateNode(XmlNodeType.Attribute, ap.Name, ns = "" ? null : ns); rootAtt.Value = ap.Value
39、; root.Attributes.SetNamedItem(rootAtt); xDoc.AppendChild(root); XmlNode ln = xDoc.CreateNode(XmlNodeType.E
40、lement, elemName, null); AppendChild(xDoc, ln, paras); root.AppendChild(ln); try
41、60; xDoc.Save(fileFullName); catch throw;
42、0; / <summary> / 創建一個XML文檔 / </summary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="rootp">根結點對象</param> / &l
43、t;param name="elemp">元素節點對象</param> / <param name="paras">XML參數</param> public static void CreateXMLFile(string fileFullName, XmlParameter rootp, XmlParameter elemp, params XmlParameter paras)
44、 XmlDocument xDoc = new XmlDocument(); XmlNode xn; xn = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null); xDoc.AppendChild(xn
45、); XmlNode root = xDoc.CreateElement(rootp.Name); string ns = rootp.NamespaceOfPrefix = null ? "" : root.GetNamespaceOfPrefix(rootp.NamespaceOfPrefix); foreach (Att
46、ributeParameter ap in rootp.Attributes) XmlNode rootAtt = xDoc.CreateNode(XmlNodeType.Attribute, ap.Name, ns = "" ? null : ns);
47、 rootAtt.Value = ap.Value; root.Attributes.SetNamedItem(rootAtt); xDoc.AppendChild(root); XmlNo
48、de ln = xDoc.CreateNode(XmlNodeType.Element, elemp.Name, null); ns = elemp.NamespaceOfPrefix = null ? "" : ln.GetNamespaceOfPrefix(elemp.NamespaceOfPrefix); foreach (AttributeParameter ap in elemp.Attribute
49、s) XmlNode elemAtt = xDoc.CreateNode(XmlNodeType.Attribute, ap.Name, ns = "" ? null : ns); elemAtt.Value = ap.Value
50、; ln.Attributes.SetNamedItem(elemAtt); AppendChild(xDoc, ln, paras); root.AppendChild(ln);
51、; try xDoc.Save(fileFullName); catch
52、60; throw; #endregion #region AddNewNode / <summary> / 添加新節點 / </summary>
53、60; / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="parentNode">新節點的父節點對象</param> / <param name="paras">XML參數對象</param> public static bool AddNewNode(st
54、ring fileFullName, XmlNode parentNode, params XmlParameter paras) XmlDocument xDoc = xmlDoc(fileFullName); if (parentNode.Name = xDoc.DocumentElement.Name)
55、 XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes0.Name, null); AppendChild(xDoc, newNode, paras); &
56、#160; xDoc.DocumentElement.AppendChild(newNode); else AddEveryNode(xDoc, par
57、entNode, paras); xDoc.Save(fileFullName); return true; / <summary> / 添加新節點 / </summary>&
58、#160; / <param name="xDoc">XmlDocument對象</param> / <param name="parentNode">新節點的父節點對象</param> / <param name="paras">XML參數對象</param> public static bool AddNewNode(XmlDoc
59、ument xDoc, XmlNode parentNode, params XmlParameter paras) if (parentNode.Name = xDoc.DocumentElement.Name) XmlNode newNode = xDoc
60、.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes0.Name, null); AppendChild(xDoc, newNode, paras); xDoc.DocumentElement.AppendChild(newNode);
61、 else AddEveryNode(xDoc, parentNode, paras); &
62、#160; return true; / <summary> / 添加新節點 / </summary> / <param name="xDoc">XmlDocument對象</param> / <param name="parentName">新節點的父節點名稱<
63、/param> / <param name="paras">XML參數對象</param> public static bool AddNewNode(XmlDocument xDoc, string parentName, params XmlParameter paras) XmlNode parentNode = GetNode(xDoc, par
64、entName); if (parentNode = null) return false; if (parentNode.Name = xDoc.DocumentElement.Name) XmlNode new
65、Node = xDoc.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes0.Name, null); AppendChild(xDoc, newNode, paras); xDoc.DocumentElement.AppendChild(newNode);&
66、#160; else AddEveryNode(xDoc, parentNode, paras);
67、160; return true; / <summary> / 添加新節點 / </summary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="parentName"
68、>新節點的父節點名稱</param> / <param name="paras">XML參數對象</param> public static bool AddNewNode(string fileFullName, string parentName, params XmlParameter paras) XmlDocument xDoc =
69、 xmlDoc(fileFullName); XmlNode parentNode = GetNode(xDoc, parentName); if (parentNode = null) return false; if (parentNode.Name = xDoc.DocumentElement.Name)
70、 XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes0.Name, null); AppendChild(xDoc, newNode, paras); &
71、#160; xDoc.DocumentElement.AppendChild(newNode); else
72、; AddEveryNode(xDoc, parentNode, paras); xDoc.Save(fileFullName); return true; #endregion #region AddAttribute
73、160; / <summary> / 添加節點屬性 / </summary> / <param name="xDoc">XmlDocument對象</param> / <param name="node">節點對象</param> / <param name="namespace
74、OfPrefix">該節點的命名空間URI</param> / <param name="attributeName">新屬性名稱</param> / <param name="attributeValue">屬性值</param> public static void AddAttribute(XmlDocument xDoc, XmlNode node, string
75、namespaceOfPrefix, string attributeName, string attributeValue) string ns = namespaceOfPrefix = null ? null : node.GetNamespaceOfPrefix(namespaceOfPrefix); XmlNode xn = xDoc.CreateNode(XmlNodeType.
76、Attribute, attributeName, ns = "" ? null : ns); xn.Value = attributeValue; node.Attributes.SetNamedItem(xn); / <summary> / 添加節點屬性
77、0; / </summary> / <param name="xDoc">XmlDocument對象</param> / <param name="node">節點對象</param> / <param name="namespaceOfPrefix">該節點的命名空間URI</param> / <par
78、am name="attps">節點屬性參數</param> public static void AddAttribute(XmlDocument xDoc, XmlNode node, string namespaceOfPrefix, params AttributeParameter attps) string ns = namespaceOfPrefix = null ? null : no
79、de.GetNamespaceOfPrefix(namespaceOfPrefix); foreach (AttributeParameter attp in attps) XmlNode xn = xDoc.CreateNode(XmlNodeType.Attribute, attp.Name,
80、 ns = "" ? null : ns); xn.Value = attp.Value; node.Attributes.SetNamedItem(xn);
81、; / <summary> / 添加節點屬性 / </summary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="node">節點對象</param> / <param name="namespaceOfP
82、refix">該節點的命名空間URI</param> / <param name="attributeName">新屬性名稱</param> / <param name="attributeValue">屬性值</param> public static void AddAttribute(string fileFullName, XmlNode node, string
83、namespaceOfPrefix, string attributeName, string attributeValue) XmlDocument xDoc = xmlDoc(fileFullName); AddAttribute(xDoc, node, namespaceOfPrefix, attributeName, attributeValue);
84、; xDoc.Save(fileFullName); / <summary> / 添加節點屬性 / </summary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="
85、node">節點對象</param> / <param name="namespaceOfPrefix">該節點的命名空間URI</param> / <param name="attps">節點屬性參數</param> public static void AddAttribute(string fileFullName, XmlNode node, string name
86、spaceOfPrefix, params AttributeParameter attps) XmlDocument xDoc = xmlDoc(fileFullName); AddAttribute(xDoc, node, namespaceOfPrefix, attps); xDoc.Save(file
87、FullName); / <summary> / 添加節點屬性 / </summary> / <param name="xDoc">XmlDocument對象</param> / <param name="nodeName">節點名稱</param>
88、0; / <param name="namespaceOfPrefix">該節點的命名空間URI</param> / <param name="attributeName">新屬性名稱</param> / <param name="attributeValue">屬性值</param> public static void AddAttribut
89、e(XmlDocument xDoc, string nodeName, string namespaceOfPrefix, string attributeName, string attributeValue) XmlNodeList xlst = xDoc.DocumentElement.ChildNodes; for (int i = 0; i < xlst.Count; i+
90、) XmlNode node = GetNode(xlsti, nodeName); if (node = null) return;
91、0; string ns = namespaceOfPrefix = null ? null : node.GetNamespaceOfPrefix(namespaceOfPrefix); XmlNode xn = xDoc.CreateNode(XmlNodeType.Attribute, attributeName, ns = "" ? null : ns);
92、; xn.Value = attributeValue; node.Attributes.SetNamedItem(xn); / <summary> / 添加節點屬性
93、; / </summary> / <param name="xDoc">XmlDocument對象</param> / <param name="nodeName">節點名稱</param> / <param name="namespaceOfPrefix">該節點的命名空間URI</param> / <
94、param name="attps">節點屬性參數</param> public static void AddAttribute(XmlDocument xDoc, string nodeName, string namespaceOfPrefix, params AttributeParameter attps) XmlNodeList xlst = xDoc.DocumentElement.Ch
95、ildNodes; for (int i = 0; i < xlst.Count; i+) XmlNode node = GetNode(xlsti, nodeName);
96、 if (node = null) return; string ns = namespaceOfPrefix = null ? null : node.GetNamespaceOfPrefix(namespaceOfPrefix); foreach (AttributeParameter attp in attps)
97、160; XmlNode xn = xDoc.CreateNode(XmlNodeType.Attribute, attp.Name, ns = "" ? null : ns);
98、160; xn.Value = attp.Value; node.Attributes.SetNamedItem(xn);
99、60; / <summary> / 添加節點屬性 / </summary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="nodeName">節點名稱</param>
100、; / <param name="namespaceOfPrefix">該節點的命名空間URI</param> / <param name="attributeName">新屬性名稱</param> / <param name="attributeValue">屬性值</param> public static void AddAttribute(strin
101、g fileFullName, string nodeName, string namespaceOfPrefix, string attributeName, string attributeValue) XmlDocument xDoc = xmlDoc(fileFullName); XmlNodeList xlst = xDoc.DocumentElement.ChildNodes;&
102、#160; for (int i = 0; i < xlst.Count; i+) XmlNode node = GetNode(xlsti, nodeName); if
103、(node = null) break; AddAttribute(xDoc, node, namespaceOfPrefix, attributeName, attributeValue); xDoc.Save(fileFullName);
104、60; / <summary> / 添加節點屬性 / </summary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="nodeName">節點名稱</param> / <param name="
105、namespaceOfPrefix">該節點的命名空間URI</param> / <param name="attps">節點屬性參數</param> public static void AddAttribute(string fileFullName, string nodeName, string namespaceOfPrefix, params AttributeParameter attps)
106、; XmlDocument xDoc = xmlDoc(fileFullName); XmlNodeList xlst = xDoc.DocumentElement.ChildNodes; for (int i = 0; i < xlst.Count; i+)
107、0; XmlNode node = GetNode(xlsti, nodeName); if (node = null) break; AddAttribute(xDoc, node, namespaceOfPref
108、ix, attps); xDoc.Save(fileFullName); #endregion #region GetNode / <summary> / 獲取指定節點名稱的節點對象 / </s
109、ummary> / <param name="fileFullName">文件名稱,包括完整路徑</param> / <param name="nodeName">節點名稱</param> / <returns></returns> public static XmlNode GetNode(string fileFullName, s
110、tring nodeName) XmlDocument xDoc = xmlDoc(fileFullName); if (xDoc.DocumentElement.Name = nodeName) return (XmlNode)xDoc.DocumentElement; XmlNodeList nlst = xDoc.DocumentElement.ChildNodes; foreach (XmlNode xns in nlst) / 遍歷所有子節點 if (xns.Name.ToLower() =
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 半潛式鉆井-生產-儲卸平臺企業數字化轉型與智慧升級戰略研究報告
- 2024年濰坊市寒亭區國有企業人才庫招聘筆試真題
- 數據整合與整合分析-全面剖析
- 2024年酒泉肅北縣公益性崗位招聘筆試真題
- 武術教學體系構建-全面剖析
- 維修培訓與教育信息化-全面剖析
- 麻醉機能量回收系統測試-全面剖析
- 航空業個性化營銷策略-全面剖析
- 安全防護體系架構設計-全面剖析
- 軟件產業生態重構-全面剖析
- 2024-2025年第二學期一年級語文教學進度表
- 3.1《百合花》課件 統編版高一語文必修上冊
- 會展營銷學知到智慧樹章節測試課后答案2024年秋上海旅游高等專科學校
- 主動脈球囊反搏術(IABP)護理
- 《關于加強中小學地方課程和校本課程建設與管理的意見》專題培訓
- 2025年中考物理押題猜想卷(蘇州卷)(全解全析)
- 《半導體行業發展歷程》課件
- 新能源開發知識培訓課件
- 精神科患者沖動傷人應急演練
- 《煤礦典型事故案例分析》培訓課件2025
- 《兒童保健學緒論》課件
評論
0/150
提交評論