Public classKeyValue {Private stringKey =string. Empty; Public stringKey {Get{returnkey;} Set{key =value;} } Private stringValue =string. Empty; Public stringValue {Get{return This. Value;} Set{ This. Value =value;} } PublicKeyValue (stringKeystringvalue) { This. Key =key; This. Value =value; } } Public classResult { Public Static stringCreateresult (stringStatusstringMessage, list<keyvalue>rslist) { Try{XmlDocument xmldoc=NewXmlDocument (); //creating a type declaration nodeXmlNode node = xmldoc.createxmldeclaration ("1.0","Utf-8",""); Xmldoc.appendchild (node); //Creating the root nodeXmlNode root = Xmldoc.createelement ("Data"); Xmldoc.appendchild (root); XmlNode result= Xmldoc.createnode (XmlNodeType.Element,"result",NULL); CreateNode (xmldoc, result,"Status", status); CreateNode (xmldoc, result,"message", message); Root. AppendChild (result); XmlNode WRITETEXT= Xmldoc.createnode (XmlNodeType.Element,"WRITETEXT",NULL); Root. AppendChild (WRITETEXT); if(Rslist! =NULL) { for(inti =0; i < Rslist.count; i++) {CreateNode (xmldoc, WRITETEXT, Rslist[i]. Key, Rslist[i]. Value); } } returnXmldoc.innerxml; } Catch(Exception) {Throw; } } Public Static voidCreateNode (XmlDocument xmldoc, XmlNode parentnode,stringNamestringvalue) {XmlNode node= Xmldoc.createnode (XmlNodeType.Element, name,NULL); Node. InnerText=value; Parentnode.appendchild (node); } Public StaticXmlDocument Createtitle (stringStatusstringmessage) { Try{XmlDocument xmldoc=NewXmlDocument (); //creating a type declaration nodeXmlNode node = xmldoc.createxmldeclaration ("1.0","Utf-8",""); Xmldoc.appendchild (node); //Creating the root nodeXmlNode root = Xmldoc.createelement ("Data"); Xmldoc.appendchild (root); XmlNode result= Xmldoc.createnode (XmlNodeType.Element,"result",NULL); CreateNode (xmldoc, result,"Status", status); CreateNode (xmldoc, result,"message", message); Root. AppendChild (result); returnxmldoc; } Catch(Exception) {Throw; } } Public Static stringCreateList (XmlDocument xmldoc,stringElementparent,stringItem, list<keyvalue>rslist) { Try{XmlNode XmlNode= Xmldoc.selectsinglenode ("Data"); XmlNode WRITETEXT=Xmlnode.selectsinglenode (elementparent); XmlNode Itemnode=NULL; if(WRITETEXT = =NULL) {WRITETEXT= Xmldoc.createnode (XmlNodeType.Element, Elementparent,NULL); Xmlnode.appendchild (WRITETEXT); Itemnode= Xmldoc.createnode (XmlNodeType.Element, item,NULL); WriteText. AppendChild (Itemnode); } Else{Itemnode= Xmldoc.createnode (XmlNodeType.Element, item,NULL); WriteText. AppendChild (Itemnode); } if(Rslist! =NULL) { for(inti =0; i < Rslist.count; i++) {CreateNode (xmldoc, Itemnode, Rslist[i]. Key, Rslist[i]. Value); } } returnXmldoc.innerxml; } Catch(Exception) {Throw; } } } Public enumResultstate {Success=1, failed =0, none =-1, error =-2, Mserror =-3 } Public classHelperxml {/// <summary> ///Parsing XML/// </summary> /// <param name= "xmltext" ></param> Public StaticXmlNode Analysisxml (stringxmlfile) { Try{System.Xml.XmlDocument xmldoc=NewXmlDocument (); Xmldoc.load (xmlfile); XmlNode Root= Xmldoc.selectsinglenode ("Master"); //Get all child nodes under the root nodeXmlNodeList nodelist =Root. ChildNodes; returnRoot; } Catch(Exception) {Throw; } } /// <summary> ///gets all nodes under the specified node/// </summary> /// <param name= "XmlNode" ></param> /// <param name= "NodeName" ></param> /// <returns></returns> Public StaticXmlNode Getappointxmlnode (XmlNode XmlNode,stringnodeName) { Try{XmlNode node=Xmlnode.selectsinglenode (nodeName); returnnode; } Catch(Exception) {Throw; } } }
C # manipulating XML