XmlDocument two create XML application instances

Source: Internet
Author: User

This is mainly about XmlDocument creating, deleting, editing, and inserting XML operations into nodes.

XmlDocument xmldoc = new XmlDocument ();
XmlNode XmlNode;
XmlElement Xmlelem;
XmlElement xmlelem2;
XmlText xmltext;

Declarative paragraphs that add XML
XmlNode = Xmldoc.createnode (Xmlnodetype.xmldeclaration, "", "");
Xmldoc.appendchild (XmlNode);
Add a root element
Xmlelem = Xmldoc.createelement ("", "Root", "");
XmlText = Xmldoc.createtextnode ("root text");
Xmlelem.appendchild (xmltext);
Xmldoc.appendchild (Xmlelem);
Add a different element
XMLELEM2 = Xmldoc.createelement ("sampleelement");
XMLELEM2 = Xmldoc.createelement ("", "Sampleelement", "");
XmlText = Xmldoc.createtextnode ("The text of the sample element");
Xmlelem2.appendchild (xmltext);
Xmldoc.childnodes.item (1). appendchild (XMLELEM2);
Save the created XML document
Try
{
Xmldoc.save ("G:/yunli/srcloud.web/uploads/filehistory/data.xml");
}
catch (Exception e)
{
Display error messages
Console.WriteLine (E.message);
}
Console.ReadLine ();
}

The above is simply created, the following example, XmlDocument create XML document and add Delete update node

using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Xml;
Namespace Xmldomdemo
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}


        private void Btnload_click (object sender, EventArgs e)
 & nbsp;      {
            XmlDocument xmldoc = new XmlDocument ();
            xmldoc.load ("books.xml");
            MessageBox.Show (xmldoc.innerxml);
       }
       //Create document
        private void Btncreate_click (object sender, EventArgs e)
        {
             XmlDocument xmldoc = new XmlDocument ();

Create a definition declaration for XML
XmlDeclaration Dec = xmldoc.createxmldeclaration ("1.0", "gb2312", null);
Xmldoc.appendchild (DEC);

To create a root node
XmlElement root = Xmldoc.createelement ("books");
Xmldoc.appendchild (root);


XmlNode book = xmldoc.createelement ("book");

XmlElement title = xmldoc.createelement ("title");
Title.innertext = "SQL Server";
Book.appendchild (title);

XmlElement ISBN = xmldoc.createelement ("ISBN");
Isbn.innertext = "444444";
Book.appendchild (ISBN);

XmlElement author = xmldoc.createelement ("author");
Author.innertext = "Jia";
Book.appendchild (author);

XmlElement price = Xmldoc.createelement ("price");
Price.innertext = "120";
Price.setattribute ("unit", "___fckpd___0quot;");

Book.appendchild (price);
Root.appendchild (book);

Xmldoc.save ("books.xml");
}

private void Btninsert_click (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load ("books.xml");

XmlNode root = Xmldoc.selectsinglenode ("books");

XmlElement book = xmldoc.createelement ("book");

XmlElement title = xmldoc.createelement ("title");
Title.innertext = "xml";
Book.appendchild (title);

XmlElement ISBN = xmldoc.createelement ("ISBN");
Isbn.innertext = "333333";
Book.appendchild (ISBN);

XmlElement author = xmldoc.createelement ("author");
Author.innertext = "Snow";
Book.appendchild (author);

XmlElement price = Xmldoc.createelement ("price");
Price.innertext = "120";
Price.setattribute ("unit", "___fckpd___0quot;");

Book.appendchild (price);

Root.appendchild (book);

Xmldoc.save ("books.xml");
MessageBox.Show ("Data has been written!") ");
}

private void Btnupdate_click (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load ("books.xml");

           //"//book[@unit =" $ "]"
            //Get all child nodes of the books node
             XmlNodeList nodelist = Xmldoc.selectsinglenode ("Books//book"). ChildNodes;

           //Traverse all child nodes
             foreach (XmlNode xn in nodelist)
             {
                //Convert child node type to XmlElement type
                 XmlElement XE = (XmlElement) xn;
                if (xe.name = = " Author ")
                {
                     Xe.innertext = "Amandag";
               }

if (Xe.getattribute ("unit") = = "___fckpd___0quot;)
{
Xe.setattribute ("unit", "¥");
}
Break
}

XmlNodeList nodelist = xmldoc.selectnodes ("Books//book");
foreach (XmlNode xn in nodelist)
//{
foreach (XmlNode x in Xn.childnodes)
//    {
To convert a child node type to a XmlElement type
XmlElement XE = (XmlElement) x;
if (Xe.name = = "Author")
//        {
Xe.innertext = "Amandag";
//        }

if (Xe.getattribute ("unit") = = "___fckpd___0quot;)
//        {
Xe.setattribute ("unit", "¥");
//        }
break;
//    }
//}
Xmldoc.save ("books.xml");
}

private void Btndelete_click (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load ("books.xml");

XmlNodeList nodelist = Xmldoc.selectsinglenode ("Books//book"). ChildNodes;

           //Traverse all child nodes
             foreach (XmlNode xn in nodelist)
             {
                //Convert child node type to XmlElement type
                 XmlElement XE = (XmlElement) xn;
                if (xe.name = = " Author ")
                {
                     Xe.removeall ();
               }

                if ( Xe.getattribute ("unit") = = "¥")
                 {
                     xe.removeattribute ("unit");
               }
           }
            xmldoc.save ("books.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.