XMLHelper(C#版)(更新)_第1頁(yè)
XMLHelper(C#版)(更新)_第2頁(yè)
XMLHelper(C#版)(更新)_第3頁(yè)
XMLHelper(C#版)(更新)_第4頁(yè)
XMLHelper(C#版)(更新)_第5頁(yè)
已閱讀5頁(yè),還剩40頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、XMLHelper(C#版)(更新)默認(rèn)分類 2008-10-07 15:39:59 閱讀1217 評(píng)論12 字號(hào):大中小 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>    / 創(chuàng)建一個(gè)XmlDocument對(duì)象  

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>    / 創(chuàng)建一個(gè)XML文檔    / </summary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="rootName">根結(jié)點(diǎn)名稱</param>    / <param

21、 name="elemName">元素節(jié)點(diǎn)名稱</param>    / <param name="paras">XML參數(shù)</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>    / 創(chuàng)建一個(gè)

26、XML文檔    / </summary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="rootName">根結(jié)點(diǎn)名稱</param>    / <param name="elemp">元素節(jié)點(diǎn)對(duì)象</param>  &

27、#160; / <param name="paras">XML參數(shù)</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>    / 創(chuàng)建一個(gè)XML文檔    / </summary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="rootp">根結(jié)點(diǎn)對(duì)象</param>    / <param name="elemName">

35、元素節(jié)點(diǎn)名稱</param>    / <param name="paras">XML參數(shù)</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>    / 創(chuàng)建一個(gè)XML文檔    / </summary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="rootp">根結(jié)點(diǎn)對(duì)象</param>    / &l

43、t;param name="elemp">元素節(jié)點(diǎn)對(duì)象</param>    / <param name="paras">XML參數(shù)</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>    / 添加新節(jié)點(diǎn)    / </summary>

53、60;   / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="parentNode">新節(jié)點(diǎn)的父節(jié)點(diǎn)對(duì)象</param>    / <param name="paras">XML參數(shù)對(duì)象</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>    / 添加新節(jié)點(diǎn)    / </summary>&

58、#160;   / <param name="xDoc">XmlDocument對(duì)象</param>    / <param name="parentNode">新節(jié)點(diǎn)的父節(jié)點(diǎn)對(duì)象</param>    / <param name="paras">XML參數(shù)對(duì)象</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>    / 添加新節(jié)點(diǎn)    / </summary>    / <param name="xDoc">XmlDocument對(duì)象</param>    / <param name="parentName">新節(jié)點(diǎn)的父節(jié)點(diǎn)名稱<

63、/param>    / <param name="paras">XML參數(shù)對(duì)象</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>    / 添加新節(jié)點(diǎn)    / </summary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="parentName"

68、>新節(jié)點(diǎn)的父節(jié)點(diǎn)名稱</param>    / <param name="paras">XML參數(shù)對(duì)象</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>    / 添加節(jié)點(diǎn)屬性    / </summary>    / <param name="xDoc">XmlDocument對(duì)象</param>    / <param name="node">節(jié)點(diǎn)對(duì)象</param>    / <param name="namespace

74、OfPrefix">該節(jié)點(diǎn)的命名空間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>    / 添加節(jié)點(diǎn)屬性  

77、0; / </summary>    / <param name="xDoc">XmlDocument對(duì)象</param>    / <param name="node">節(jié)點(diǎn)對(duì)象</param>    / <param name="namespaceOfPrefix">該節(jié)點(diǎn)的命名空間URI</param>    / <par

78、am name="attps">節(jié)點(diǎn)屬性參數(shù)</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>    / 添加節(jié)點(diǎn)屬性    / </summary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="node">節(jié)點(diǎn)對(duì)象</param>    / <param name="namespaceOfP

82、refix">該節(jié)點(diǎn)的命名空間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>    / 添加節(jié)點(diǎn)屬性    / </summary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="

85、node">節(jié)點(diǎn)對(duì)象</param>    / <param name="namespaceOfPrefix">該節(jié)點(diǎn)的命名空間URI</param>    / <param name="attps">節(jié)點(diǎn)屬性參數(shù)</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>    / 添加節(jié)點(diǎn)屬性    / </summary>    / <param name="xDoc">XmlDocument對(duì)象</param>    / <param name="nodeName">節(jié)點(diǎn)名稱</param> 

88、0;  / <param name="namespaceOfPrefix">該節(jié)點(diǎn)的命名空間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>    / 添加節(jié)點(diǎn)屬性   

93、; / </summary>    / <param name="xDoc">XmlDocument對(duì)象</param>    / <param name="nodeName">節(jié)點(diǎn)名稱</param>    / <param name="namespaceOfPrefix">該節(jié)點(diǎn)的命名空間URI</param>    / <

94、param name="attps">節(jié)點(diǎn)屬性參數(shù)</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>    / 添加節(jié)點(diǎn)屬性    / </summary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="nodeName">節(jié)點(diǎn)名稱</param>   

100、; / <param name="namespaceOfPrefix">該節(jié)點(diǎn)的命名空間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>    / 添加節(jié)點(diǎn)屬性    / </summary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="nodeName">節(jié)點(diǎn)名稱</param>    / <param name="

105、namespaceOfPrefix">該節(jié)點(diǎn)的命名空間URI</param>    / <param name="attps">節(jié)點(diǎn)屬性參數(shù)</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>    / 獲取指定節(jié)點(diǎn)名稱的節(jié)點(diǎn)對(duì)象    / </s

109、ummary>    / <param name="fileFullName">文件名稱,包括完整路徑</param>    / <param name="nodeName">節(jié)點(diǎn)名稱</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)  / 遍歷所有子節(jié)點(diǎn)                    if (xns.Name.ToLower() =

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論