C # Create an instance code share that reads and writes to an XML file

Source: Internet
Author: User
This article mainly introduced the C # creation, reads, writes the XML file The method, has the very good reference value. Let's take a look at the little series.

The XML file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <Advertisements> <Ad> <imageurl>001.jpg</imageurl > <NavigateUrl>001.aspx</NavigateUrl> <Impressions>10</Impressions> <Keyword> gucas01</keyword> </Ad> <Ad> <ImageUrl>002.jpg</ImageUrl> <navigateurl>002. aspx</navigateurl> <Impressions>20</Impressions> <Keyword>gucas02</Keyword> </ad > <Ad> <ImageUrl>003.jpg</ImageUrl> <NavigateUrl>003.aspx</NavigateUrl> < impressions>30</impressions> <Keyword>gucas03</Keyword> </Ad> <Ad> <imageurl >004.jpg</ImageUrl> <NavigateUrl>004.aspx</NavigateUrl> <impressions>40</ impressions> <Keyword>gucas04</Keyword> </Ad> <Ad> <imageurl>005.jpg</imageurl > <NavigateUrl>005.aspx</NavigateUrl> <impressions>50</impressions> <Keyword>gucas05</Keyword> </Ad> <Ad> <imageurl>google.gif</imageurl > <NavigateUrl>http://www.google.com</NavigateUrl> <Impressions>30</Impressions> < Keyword>google</keyword> </Ad> </Advertisements>

operations on XML files

<summary>//Create XML file///</summary>//<param name= "filename" ></param> public void creat   Exmlfile (string filename) {XmlDocument xmldoc = new XmlDocument ();   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", "ZHENGYD@GUCAS.AC,CN");   CreateNode (xmldoc, Root, "Url", "WWW.GUCAS.AN,CN");   CreateNode (xmldoc, Root, "age", "27"); try {xmldoc.    Save (Server.MapPath (filename));   Response.Write ("Create XML file Myxml.xml succeeded!"); } catch (Exception ex) {Response.Write (ex.   Message); }}///<summary> Write XML file////</summary>//<param name= "file" ></param> public void Wri   Texmlfile (string file) {XmlDocument Xmdoc = new XmlDocument (); 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", "30");     CreateNode (Xmdoc, node, "Keyword", "Google"); Root.    AppendChild (node); } xmdoc.    Save (Server.MapPath (file)); Response.Write ("Write XML file XMLFile.xml succeeded.   <br> "); } catch (Exception ex) {Response.Write (ex.   Message); }}///<summary>//Create Nodes///</summary>//<param name= "xmldoc" ></param>//<param NA Me= "ParentNode" ></param>//<param name= "name" ></param>//<param name= "value" ></ param> public void CreateNode (XmlDocument xmldoc, XmlNode parentnode, string name, String value) {XmlNode node = x Mldoc.   CreateNode (xmlnodetype.element, name, NULL); Node.   InnerText = value; ParentNode. AppendChild (nODE); }///<summary>///Read XML file///</summary>//<param name= "file" ></param> private void READF   Ile (string file) {XmlDocument xmldoc = new XmlDocument (); try {xmldoc.    Load (Server.MapPath (file)); XmlNode node = xmldoc.    selectSingleNode ("advertisements");     if (node! = null) {TreeNode root = new TreeNode (); Root. Text = node.     Name;     TVXML.NODES.ADD (root); foreach (XmlNode xnode in xmldoc.      SelectNodes ("Advertisements/ad")) {TreeNode tnode = new TreeNode (); Tnode. Text = Server.HTMLEncode ("<" + XNode.      Name + ">"); Root.       Childnodes.add (tnode); foreach (XmlNode xcnode in XNode.        ChildNodes) {TreeNode Tcnode = new TreeNode (); Tcnode. Text = Server.HTMLEncode ("<" + Xcnode. Name + ">" + xcnode. InnerText + "</" + Xcnode.        Name + ">"); Tnode.       Childnodes.add (Tcnode);      } TreeNode Ttnode = new TreeNode (); Ttnode. Text = Server.HTMLEncode ("</" + xnode. Name + ""); Root.     Childnodes.add (Ttnode);     } TreeNode Tpnode = new TreeNode (); Tpnode. Text = Server.HTMLEncode ("</" + node.     Name + ">");    TVXML.NODES.ADD (Tpnode); }} catch (Exception ex) {Response.Write (ex).   Message); }  }
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.