当前位置:首页 > C#教程 > C#高级

C#:XML操作类


              1             using              system;               2             using              system.data;               3             using              system.configuration;               4             using              system.web;               5             using              system.web.security;               6             using              system.web.ui;               7             using              system.web.ui.webcontrols;               8             using              system.web.ui.webcontrols.webparts;               9             using              system.web.ui.htmlcontrols;              10             using              system.xml;              11              12             namespace              putiancheng              13             {              14             ///             <summary>              15             ///              xmlhelper 的摘要说明              16             ///             </summary>              17             public             class              xmlhelper              18                 {              19             public              xmlhelper()              20                     {              21                     }              22              23             ///             <summary>              24             ///              读取数据              25             ///             </summary>              26             ///             <param name="path">             路径             </param>              27             ///             <param name="node">             节点             </param>              28             ///             <param name="attribute">             属性名,非空时返回该属性值,否则返回串联值             </param>              29             ///             <returns>             string             </returns>              30             /*             *************************************************              31                      * 使用示列:              32                      * xmlhelper.read(path, "/node", "")              33                      * xmlhelper.read(path, "/node/element[@attribute=‘name‘]", "attribute")              34                      ***********************************************             */              35             public             static             string read(string path, string node, string attribute) 36        { 37string value = ""; 38try 39            { 40                 xmldocument doc = new xmldocument(); 41                doc.load(path); 42                 xmlnode xn = doc.selectsinglenode(node); 43                 value = (attribute.equals("") ? xn.innertext : xn.attributes[attribute].value); 44            } 45catch { } 46return value; 47        } 48 49///<summary> 50/// 插入数据 51///</summary> 52///<param name="path">路径</param> 53///<param name="node">节点</param> 54///<param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param> 55///<param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param> 56///<param name="value"></param> 57///<returns></returns> 58/************************************************** 59         * 使用示列: 60         * xmlhelper.insert(path, "/node", "element", "", "value") 61         * xmlhelper.insert(path, "/node", "element", "attribute", "value") 62         * xmlhelper.insert(path, "/node", "", "attribute", "value") 63         ************************************************/ 64publicstaticvoid insert(string path, string node, string element, string attribute, string value) 65        { 66try 67            { 68                 xmldocument doc = new xmldocument(); 69                doc.load(path); 70                 xmlnode xn = doc.selectsinglenode(node); 71if (element.equals("")) 72                { 73if (!attribute.equals("")) 74                    { 75                         xmlelement xe = (xmlelement)xn; 76                        xe.setattribute(attribute, value); 77                    } 78                } 79else 80                { 81                     xmlelement xe = doc.createelement(element); 82if (attribute.equals("")) 83                         xe.innertext = value; 84else 85                        xe.setattribute(attribute, value); 86                    xn.appendchild(xe); 87                } 88                doc.save(path); 89            } 90catch { } 91        } 92 93///<summary> 94/// 修改数据 95///</summary> 96///<param name="path">路径</param> 97///<param name="node">节点</param> 98///<param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param> 99///<param name="value"></param>100///<returns></returns>101/**************************************************102         * 使用示列:103         * xmlhelper.insert(path, "/node", "", "value")104         * xmlhelper.insert(path, "/node", "attribute", "value")105         ************************************************/106publicstaticvoid update(string path, string node, string attribute, string value)107        {108try109            {110                 xmldocument doc = new xmldocument();111                doc.load(path);112                 xmlnode xn = doc.selectsinglenode(node);113                 xmlelement xe = (xmlelement)xn;114if (attribute.equals(""))115                     xe.innertext = value;116else117                    xe.setattribute(attribute, value);118                doc.save(path);119            }120catch { }121        }122123///<summary>124/// 删除数据125///</summary>126///<param name="path">路径</param>127///<param name="node">节点</param>128///<param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>129///<param name="value"></param>130///<returns></returns>131/**************************************************132         * 使用示列:133         * xmlhelper.delete(path, "/node", "")134         * xmlhelper.delete(path, "/node", "attribute")135         ************************************************/136publicstaticvoid delete(string path, string node, string attribute)137        {138try139            {140                 xmldocument doc = new xmldocument();141                doc.load(path);142                 xmlnode xn = doc.selectsinglenode(node);143                 xmlelement xe = (xmlelement)xn;144if (attribute.equals(""))145                    xn.parentnode.removechild(xn);146else147                    xe.removeattribute(attribute);148                doc.save(path);149            }150catch { }151        }152153///<summary>154/// 获取数据集合155///</summary>156///<param name="path">路径</param>157///<param name="node">节点</param>158///<param name="value"></param>159///<returns>dictionary集合</returns>160publicstatic dictionary<int, list<string>> readingxml(string path,string node)161         {162              xmldocument xmldoc = new xmldocument();163             xmldoc.loadxml(path);164              xmlnodelist nodelist = xmldoc.selectsinglenode(node).childnodes;//item 节点165166              list<string> list = null;167              dictionary<int, list<string>> dic = new dictionary<int, list<string>>();168int key = 0;169//遍历所有子节点170foreach (xmlnode xn in nodelist)171             {172                  xmlelement xe = (xmlelement)xn;  //item173                  xmlnodelist sublist = xe.childnodes;//item的子节点 174175                  list = new list<string>();176foreach (xmlnode xmlnode in sublist)177                 {178//获取数据179                     list.add(xmlnode.innertext);180                 }181                 dic.add(key, list);182                  key++;183             }184return dic;185         }                    186    }187 }                                                    

 


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!