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< ;/firstname> ≪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> <LISTITEM&G
T <ListItemID>2</ListItemID> <DataBindCateID>1</DataBindCateID> <datavaluefield>new V alue</datavaluefield> <datatextfield>new text</datatextfield> <DataColorField>new color< ;/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");