To update information in an XML node by using LINQ

Source: Internet
Author: User
Tags foreach

The XML is as follows

<?xml version= "1.0" encoding= "Utf-8"?> <friends> <friend> <name> <firstname>guo& lt;/firstname> <lastName>Hu</lastName> </name> <address> <province>sh
    anghai</province> <city>PuDong</city> </address> </friend> <friend>
    <name> <firstName>Lei</firstName> <lastName>Hu</lastName> </name>
  <address> <province>hubei</province> <city>xiantao</city> </address> </friend> <friend> <name> <firstName>JunWen</firstName> <lastname>l i</lastname> </name> <address> <province>hubei</province> <city>wu han</city> </address> </friend> <friend> <name> <firstname>jinhao&lt ;/firstname> &Lt;lastname>liu</lastname> </name> <address> <province>ShanXi</province> <city>Taiyuan</city> </address> </friend> <friend> <name> <fir
      stname>cheng</firstname> <lastName>Fang</lastName> </name> <address> 
  <province>guangDong</province> <city>guangZhou</city> </address> </friend> <friend> <name> <firstName>Cheng</firstName> <lastname>fang</lastname&
    Gt
    </name> <address> <province>HuBei</province> <city>xianTao</city> </address> </friend> </friends>
The condition is that FirstName is cheng,lastname for Fang Modification province for Hubei,city for Wuhan

		XElement Elements = xelement.load (Server.MapPath (". /linqxml/linq1.xml "));

		var elements = from element in elements.descendants ("friend")
						where
						(from the El in
						element. Elements ("name")
						where
							el. Element ("FirstName"). Value = = "Cheng"
							&& el. Element ("LastName"). Value = = "Fang"
						select El
						). Any ()
						select element. Element ("Address");
		if (elements! = null)
		{
			elements. ToList (). ForEach (q =
				{
					q.setelementvalue ("province", "Hubei");
					Q.setelementvalue ("City", "Wuhan");
				});
		Elements.save (@ "D:\Practise\RegexDemo\Linqxml\linq1.xml");
2.

The XML is as follows

<DataBind> <ListItem> <ListItemID>1</ListItemID> <databindcateid>1</databindcat Eid> <DataValueField>1</DataValueField> <DataTextField> Order issues </DataTextField> <dat acolorfield> #F70909 </DataColorField> <ParentID>0</ParentID> </ListItem> &LT;LISTITEM&G
    T <ListItemID>2</ListItemID> <DataBindCateID>1</DataBindCateID> <datavaluefield>new V alue</datavaluefield> <datatextfield>new text</datatextfield> <DataColorField>new color&lt ;/datacolorfield> <ParentID>1</ParentID> </ListItem> <ListItem> <listitemid>3  </ListItemID> <DataBindCateID>1</DataBindCateID> <DataValueField> Dear customer, We

			has temporarily held your order.

			Would share us why do you want to cancel your order? If there ' s problem with the SKU, QuanTity or the shipping address,we can update it for you. </DataValueField> <DataTextField> ask for order cancellation reasons </DataTextField> <DataColorField> #000000 </ datacolorfield> <ParentID>2</ParentID> </ListItem> </DataBind>

Modify the value of Datacolorfield,datatextfield,datavaluefield for a query condition of Listitemid to 2

		XElement Elements = xelement.load (Server.MapPath (". /linqxml/linq1.xml "));

		var elements = from element in Elements.descendants ("ListItem")
					   where element. Element ("Listitemid"). Value = = "2"
					   select element;

		if (elements! = null)
		{
			elements. ToList (). ForEach (q =
				{
					q.setelementvalue ("Datacolorfield", "Your Color");
					Q.setelementvalue ("DataTextField", "Your Text");
					Q.setelementvalue ("DataValueField", "Your Value");
				});
		Elements.save (@ "D:\Practise\RegexDemo\Linqxml\linq1.xml");





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.