C # Create, read, write XML file 1

Source: Internet
Author: User

The XML format is as follows

<?xml version="1.0"encoding="Utf-8"?><advertisements> <Ad> <ImageUrl>001.jpg</imageurl> <NavigateUrl>001.aspx</navigateurl> <Impressions>Ten</Impressions> <Keyword>gucas01</Keyword> </Ad> <Ad> <ImageUrl>002.jpg</imageurl> <NavigateUrl>002.aspx</navigateurl> <Impressions> -</Impressions> <Keyword>gucas02</Keyword> </Ad> <Ad> <ImageUrl>003.jpg</imageurl> <NavigateUrl>003.aspx</navigateurl> <Impressions> -</Impressions> <Keyword>gucas03</Keyword> </Ad> <Ad> <ImageUrl>004.jpg</imageurl> <NavigateUrl>004.aspx</navigateurl> <Impressions> +</Impressions> <Keyword>gucas04</Keyword> </Ad> <Ad> <ImageUrl>005.jpg</imageurl> <NavigateUrl>005.aspx</navigateurl> <Impressions> -</Impressions> <Keyword>gucas05</Keyword> </Ad> <Ad> <imageurl>google.gif</ Imageurl> <navigateurl>http://www.google.com</navigateurl><Impressions> -</Impressions> <Keyword>google</Keyword> </Ad></Advertisements>

Operations on XML files

/// <summary>    ///Creating an XML file/// </summary>    /// <param name= "filename" ></param>     Public voidCreatexmlfile (stringfilename) {XmlDocument xmldoc=NewXmlDocument ();        XmlNode node; Node= xmldoc. Createxmldeclaration ("1.0","Utf-8",NULL); XmlDoc.        AppendChild (node); XmlNode Root= xmldoc. CreateElement ("Users"); XmlDoc.        AppendChild (root); CreateNode (xmldoc, Root,"UserName","zhengyd"); CreateNode (xmldoc, Root,"Email","[Email PROTECTED],CN"); CreateNode (xmldoc, Root,"URL","WWW.GUCAS.AN,CN"); CreateNode (xmldoc, Root," Age"," -"); Try{xmldoc.            Save (Server.MapPath (filename)); Response.Write ("Create XML file Myxml.xml success!"); }        Catch(Exception ex) {Response.Write (ex).        Message); }    }    /// <summary>    ///Write XML file/// </summary>    /// <param name= "file" ></param>     Public voidWritexmlfile (stringfile) {XmlDocument Xmdoc=NewXmlDocument (); Try{Xmdoc.            Load (Server.MapPath (file)); XmlNode Root= Xmdoc. selectSingleNode ("Advertisements"); if(Root! =NULL) {XmlNode node= Xmdoc. CreateNode (XmlNodeType.Element,"Ad",NULL); CreateNode (Xmdoc, node,"IMAGEURL","Google.gif"); CreateNode (Xmdoc, node,"NAVIGATEURL","http://www.google.com"); CreateNode (Xmdoc, node,"Impressions"," -"); CreateNode (Xmdoc, node,"Keyword","Google"); Root.            AppendChild (node); } xmdoc.            Save (Server.MapPath (file)); Response.Write ("writing XML file XMLFile.xml succeeded. <br>"); }        Catch(Exception ex) {Response.Write (ex).        Message); }    }    /// <summary>    ///Create a node/// </summary>    /// <param name= "xmldoc" ></param>    /// <param name= "parentnode" ></param>    /// <param name= "name" ></param>    /// <param name= "value" ></param>     Public voidCreateNode (XmlDocument xmldoc, XmlNode parentnode,stringNamestringvalue) {XmlNode node= xmldoc. CreateNode (XmlNodeType.Element, name,NULL); Node. InnerText=value; ParentNode.    AppendChild (node); }    /// <summary>    ///reading an XML file/// </summary>    /// <param name= "file" ></param>    Private voidReadFile (stringfile) {XmlDocument xmldoc=NewXmlDocument (); Try{xmldoc.            Load (Server.MapPath (file)); XmlNode node= xmldoc. selectSingleNode ("Advertisements"); if(Node! =NULL) {TreeNode root=NewTreeNode (); Root. Text=node.                Name;                TVXML.NODES.ADD (root); foreach(XmlNode XNodeinchXmlDoc. SelectNodes ("advertisements/ad") {TreeNode tnode=NewTreeNode (); Tnode. Text= Server.HTMLEncode ("<"+ XNode. Name +">"); Root.                        Childnodes.add (tnode); foreach(XmlNode XcnodeinchXNode. ChildNodes) {TreeNode Tcnode=NewTreeNode (); Tcnode. Text= Server.HTMLEncode ("<"+ Xcnode. Name +">"+ Xcnode. InnerText +"</"+ Xcnode. Name +">"); Tnode.                        Childnodes.add (Tcnode); } TreeNode Ttnode=NewTreeNode (); Ttnode. Text= Server.HTMLEncode ("</"+ XNode. Name +""); Root.                Childnodes.add (Ttnode); } TreeNode Tpnode=NewTreeNode (); Tpnode. Text= Server.HTMLEncode ("</"+ node. Name +">");            TVXML.NODES.ADD (Tpnode); }        }        Catch(Exception ex) {Response.Write (ex).        Message); }    }

C # Create, read, write XML file 1

Related Article

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.