Refer to the document-C # How to Learn From LINQ to XML (create and edit an XML tree ).

Source: Internet
Author: User
Use LINQ to create an XML tree and modify the values of elements or attributes in the tree.

Using system;

Using system. Collections. Generic;

Using system. LINQ;

Using system. text;

Using system. xml. LINQ;

Using system. xml;

Namespace linqtoxmlstudy

{

//************************************** **************************************** *****************************

// Module: program. CS

// Author: factly

// Create Date: 2008-06-30

//************************************** **************************************** *****************************

Class Program

{

Static void main (string [] ARGs)

{

// Create an XML tree struct using LINQ ------------------------------------------------------------------------------------

Xelement xmltree = new xelement ("contacts ",

New xelement ("Contact ",

New xelement ("name", "Patrick Hines "),

New xelement ("phone", "206-555-0144 ",

New xattribute ("type", "home ")),

New xelement ("phone", "425-555-0145 ",

New xattribute ("type", "work ")),

New xelement ("Address ",

New xelement ("street1", "123 Main St "),

New xelement ("city", "Mercer Island "),

New xelement ("State", "wa "),

New xelement ("Postal", "68042 ")

)

)

);

Console. writeline (xmltree );

// Use LINQ to create an XML tree and end -------------------------------------------------------------------------------

// Modify the attribute value in XML ---------------------------------------------------------------------------------

Ienumerable <system. xml. LINQ. xelement> Xe = xmltree. descendants ("phone"); // query all sets with the element name "phone" and query the time zone in case.

VaR v = Xe. Where (P => P. Attribute ("type"). value = "home ");

Foreach (VAR s in V)

{

S. setvalue ("hello ");

}

Console. writeline ("the modified result is :");

Console. writeline (xmltree );

// Modify the attribute value in XML to end -----------------------------------------------------------------------------

Console. readkey ();

}

}

}

Http://hi.baidu.com/xbs729/blog/item/30de2c266839b627d407429c.html

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.