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 |