LINQ FOR XML Simple example

Source: Internet
Author: User

LINQ, language-integrated queries (Language Integrated query) is a set of extensions for the C # and Visual Basic languages. It allows developers to manipulate memory data in the same way as querying a database. Technically, LINQ defines about 40 query operators, such as Select, from, in, where, and order by (C #). Use these operators to write query statements. However, these queries can also be based on many types of data, and each data type requires a separate LINQ type.

This article provides a brief introduction to the basic operation of XML using LINQ technology, which consists of two classes: Xmlelementinfor and Xmlhelper, and the code is implemented as follows:

     Public classXmlhelper {Private string_filepath;  PublicXmlhelper (stringFilePath) {_filepath=FilePath; }        /// <summary>        ///Creating an XML file/// </summary>         Public voidcreatedocument () {XDocument Xdoc=NewXDocument (NewXdeclaration ("1.0","Utf-8","Yes"),                NewXElement ("Root")); Xdoc.        Save (_filepath); }        /// <summary>        ///adding elements to XML and nodes/// </summary>        /// <param name= "NodeName" >element name</param>        /// <param name= "xelements" >element properties and content</param>         Public voidAddxmlnodetoroot (stringNodeName,params Object[] xelements) {XElement RootNode=xelement.load (_filepath); XElement NewNode=NewXElement (NodeName, xelements);            Rootnode.add (NewNode);        Rootnode.save (_filepath); }        /// <summary>        ///deleting elements and their child elements from XML and nodes/// </summary>        /// <param name= "NodeName" >element name</param>        /// <param name= "Idelement" >element Filter Criteria</param>         Public voidDeletenodefromroot (stringNodeName, Xmlelementinfor idelement) {XElement RootNode=xelement.load (_filepath); IEnumerable<XElement> targetnodes = fromTargetinchrootnode.descendants (nodeName)whereTarget. Element (Idelement.name). Value.equals (Idelement.value)SelectTarget;            Targetnodes.remove ();        Rootnode.save (_filepath); }        /// <summary>        ///Updating elements/// </summary>        /// <param name= "NodeName" >element name</param>        /// <param name= "Idelement" >element Filter Criteria</param>        /// <param name= "Updatevalue" >need to update content</param>         Public voidUpdatenodefromroot (stringNodeName, Xmlelementinfor idelement, xmlelementinfor[] updatevalue) {XElement RootNode=xelement.load (_filepath); IEnumerable<XElement> targetnodes = fromTargetinchrootnode.descendants (nodeName)whereTarget. Element (Idelement.name). Value.equals (Idelement.value)SelectTarget; foreach(XElement nodeinchtargetnodes) {                foreach(varIteminchupdatevalue) {node. Element (item. Name). SetValue (item.                Value);        }} rootnode.save (_filepath); }        /// <summary>        ///querying elements from the root node/// </summary>        /// <param name= "NodeName" >element name</param>        /// <param name= "Idelement" >element Filter Criteria</param>        /// <returns></returns>         PublicIenumerable<xelement> Querynodefromroot (stringNodeName, Xmlelementinfor idelement) {XElement RootNode=xelement.load (_filepath); IEnumerable<XElement> targetnodes = fromTargetinchrootnode.descendants (nodeName)whereTarget. Element (Idelement.name). Value.equals (Idelement.value)SelectTarget; returnTargetnodes; }    }
Xmlhelper
     Public classXmlelementinfor {Private string_name; Private string_value;  PublicXmlelementinfor (stringElementName,stringelementvalue) {_name=elementname; _value=Elementvalue; }         Public stringName {Get{return_name;} }         Public stringValue {Get{return_value;} }    }

LINQ FOR XML Simple example

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.