C # manipulating XML

Source: Internet
Author: User

     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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.