LINQ to XML, linqtoxml

Source: Internet
Author: User

LINQ to XML, linqtoxml

You can use LINQ to XML in two ways. The first method is as a simplified XML operation API, and the second method is to use the LINQ query tool. The following is the first method.

Main Content: add, delete, modify, and query the XML file using the LINQ query statement.

Advantages of this method:

1. We can use a single statement to create an XML tree from top to bottom.

2. We can use the XML document containing the tree to create and operate XML in the memory.

3. We can not use Text subnodes to create and operate string nodes.

4. The biggest difference (Improvement) is that when you search for an XML tree, you do not need to traverse it. Instead, you only need to query the tree and make it return the desired result.

Here I will take a notepad as an example: the Content of the notepad includes: Id (number), Title (Title), Content (Content ).

The following code is used:

/// <Summary>
/// Create and instantiate the XmlDocument
/// </Summary>
XDocument xml = XDocument. Load (xmlPath );

  

/// <Summary>
/// Xml file path
/// </Summary>
Const string xmlPath = @ "... \ DAL \ Notepad \ NotepadXML \ Notepad. xml ";

  

/// <Summary>
/// Add notepad
/// </Summary>
/// <Param name = "title"> title </param>
/// <Param name = "content"> content </param>
/// <Returns> true: Successful; false: Failed </returns>
Public bool addNotepad (string number, string title, string content)
{
Try
{
XElement xd = xml. Root;
XElement xm = new XElement ("Notepad ",
New XElement ("Id", number ),
New XElement ("Title", title ),
New XElement ("Content", content )));
Xd. Add (xm );
Xml. Save (xmlPath );
}
Catch
{
Return false;
}
Return true;
}
# Endregion

 

/// <Summary>
/// Delete a notepad
/// </Summary>
/// <Param name = "NotepadNumber"> notepad No. </param>
/// <Returns> true: Successful; false: Failed </returns>
Public bool delectTheNotepad (string NotepadNumber)
{
Try
{
XElement xd = xml. Root;
Xd. Elements (). First (n => n. Element ("Id"). Value = NotepadNumber). Remove ();
Xd. Save (xmlPath );
}
Catch
{
Return false;
}
Return true;
}

 

/// <Summary>
/// Modify notepad
/// </Summary>
/// <Param name = "NotepadNumber"> notepad id </param>
/// <Param name = "title"> title </param>
/// <Param name = "content"> content </param>
/// <Returns> true: Successful; false: Failed </returns>
Public bool updateNotepad (string NotepadNumber, string title, string content)
{
Try
{
XElement xd = xml. Root;
XElement xd2 = xd. Elements (). First (n => n. Element ("Id"). Value = NotepadNumber );
Xd2.Element ("Title"). Value = title;
Xd2.Element ("Content"). Value = content;
Xd. Save (xmlPath );
}
Catch
{
Return false;
}
Return true;
}

 

/// <Summary>
/// Obtain all the notebooks in a file
/// </Summary>
/// <Returns> All notebooks in a file </returns>
Public List <string []> getAllNotepadInFile ()
{
List <string []> list = new List <string []> ();
XElement xd = xml. Root;
Foreach (XElement item in xd. Elements ())
{
List. Add (new string [] {
Item. Element ("Id"). Value,
Item. Element ("Title"). Value,
Item. Element ("Content"). Value
});
}
Return list;
}

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.