C # xml,xmldocument Simple Operation Example

Source: Internet
Author: User

     Private Static string " /data/bookstore.xml ";

1. Adding nodes

/// <summary>///inserting a node into the root node/// </summary> Public Static voidAddOne () {XmlDocument xmldoc=NewXmlDocument ();    Xmldoc.load (_store); //1. Find the Booksotre nodeXmlNode root = Xmldoc.selectsinglenode ("Bookstore"); //2. Create a book nodeXmlElement book = Xmldoc.createelement (" Book"); Book. SetAttribute ("genre","Lizanhong"); Book. SetAttribute ("ISBN","2-3431-4"); XmlElement title= Xmldoc.createelement ("title"); Title. InnerText="C # Getting Started classic"; Book.    AppendChild (title); XmlElement author= Xmldoc.createelement ("author"); Author. InnerText="Houjie"; Book.    AppendChild (author); XmlElement Price= Xmldoc.createelement (" Price"); Price. InnerText="58.3"; Book.    AppendChild (price); //Add the book node to the root nodeRoot.    AppendChild (book); //Save ContentXmldoc.save (_store);}

2. Modify the Node

/// <summary>///Modify Node/// </summary> Public Static voidUpdateone () {XmlDocument doc=NewXmlDocument (); Doc.    Load (_store); //Traverse ModificationXmlNodeList nodeList = doc. selectSingleNode ("Bookstore").    ChildNodes; foreach(XmlNode nodeinchnodeList) {        //To Convert a child node type to a xmleletment typeXmlElement ele =(XmlElement) node; if(Ele. GetAttribute ("genre") =="Lizanhong") {ele. SetAttribute ("genre","udpate Tribute to the red"); XmlNodeList NodeList2=Ele.            ChildNodes; foreach(XmlNode Node2inchnodeList2) {XmlElement Ele2=(XmlElement) Node2; if(Ele2. Name = ="author") {Ele2. InnerText="deferred";  Break; }            }             Break; }    }    //Save ChangesDoc. Save (_store);}/// <summary>///Modify Node 2, using XPath/// </summary> Public Static voidUpdatetwo () {XmlDocument doc=NewXmlDocument (); Doc.    Load (_store); //Query Node//XmlNode root = Doc.    selectSingleNode ("bookstore"); //XmlNodeList books = doc. SelectNodes ("Bookstore/book");XmlNode title = Doc. SelectNodes ("Bookstore/book/title")[0]; Title. InnerText= title. InnerText +"---XPath"; Doc. Save (_store);}

3. Delete a node

/// <summary>///Delete node, properties, Content/// </summary> Public Static voidDeleteone () {XmlDocument doc=NewXmlDocument (); Doc.    Load (_store); XmlNodeList NodeList= Doc. selectSingleNode ("Bookstore").    ChildNodes; foreach(varIteminchnodeList) {XmlElement Ele=(XmlElement) item; if(Ele. GetAttribute ("genre") =="Fantasy")        {            //Delete PropertyEle. RemoveAttribute ("genre"); }        Else if(Ele. GetAttribute ("genre") =="udpate Tribute to the red")        {            //Delete the entire contents of the nodeEle.        RemoveAll (); }    }    //Save ChangesDoc. Save (_store);}/// <summary>///Delete Empty nodes/// </summary> Public Static voidDeletetwo () {XmlDocument doc=NewXmlDocument (); Doc.    Load (_store); XmlNode Root= Doc. selectSingleNode ("Bookstore"); XmlNodeList NodeList=Root.    ChildNodes; foreach(XmlNode nodeinchnodeList) {XmlElement Ele=(XmlElement) node; if(Ele. Childnodes.count <=0)            //only immediate child nodes can be deletedRoot.    RemoveChild (node); } doc. Save (_store);}

4. Query List

/// <summary>///Show all the data/// </summary> Public Static voidShowone () {XmlDocument doc=NewXmlDocument (); Doc.    Load (_store); XmlNode Root= Doc. selectSingleNode ("Bookstore"); XmlNodeList NodeList=Root.    ChildNodes; foreach(varNodeinchnodeList) {XmlElement Ele=(XmlElement) node; Console.WriteLine (Ele. GetAttribute ("genre")); Console.WriteLine (Ele. GetAttribute ("ISBN")); XmlNodeList NodeList2=Ele.        ChildNodes; foreach(XmlNode Node2inchnodeList2) {Console.WriteLine (node2).        InnerText); }    }}

C # xml,xmldocument Simple Operation Example

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.