C # Create, read, write XML file

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>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>
Creating an XML file
</summary>
<param name= "filename" ></param>
public void Createxmlfile (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", "[email PROTECTED],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 Writexmlfile (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 a Node
</summary>
<param name= "XmlDoc" ></param>
<param name= "ParentNode" ></param>
<param name= "Name" ></param>
<param name= "Value" ></param>
public void CreateNode (XmlDocument xmldoc, XmlNode parentnode, string name, String value)
{
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 void ReadFile (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);
}
}

C # Create, read, write XML file

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.