XML 如下
<?xml version="1.0" encoding="utf-8"?>
<friends>
<friend>
<name>
<firstName>Guo</firstName>
<lastName>Hu</lastName>
</name>
<address>
<province>Shanghai</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>Li</lastName>
</name>
<address>
<province>hubei</province>
<city>wuhan</city>
</address>
</friend>
<friend>
<name>
<firstName>Jinhao</firstName>
<lastName>Liu</lastName>
</name>
<address>
<province>ShanXi</province>
<city>Taiyuan</city>
</address>
</friend>
<friend>
<name>
<firstName>Cheng</firstName>
<lastName>Fang</lastName>
</name>
<address>
<province>guangDong</province>
<city>guangZhou</city>
</address>
</friend>
<friend>
<name>
<firstName>Cheng</firstName>
<lastName>Fang</lastName>
</name>
<address>
<province>HuBei</province>
<city>xianTao</city>
</address>
</friend>
</friends>
條件是firstName爲Cheng,lastname爲Fang的修改province 爲HuBei,city爲WuHan XElement Elements = XElement.Load(Server.MapPath("../Linqxml/linq1.xml"));
var elements = from element in Elements.Descendants("friend")
where
(
from 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.XML 如下
<DataBind>
<ListItem>
<ListItemID>1</ListItemID>
<DataBindCateID>1</DataBindCateID>
<DataValueField>1</DataValueField>
<DataTextField>訂單問題</DataTextField>
<DataColorField>#F70909</DataColorField>
<ParentID>0</ParentID>
</ListItem>
<ListItem>
<ListItemID>2</ListItemID>
<DataBindCateID>1</DataBindCateID>
<DataValueField>new value</DataValueField>
<DataTextField>new text</DataTextField>
<DataColorField>new color</DataColorField>
<ParentID>1</ParentID>
</ListItem>
<ListItem>
<ListItemID>3</ListItemID>
<DataBindCateID>1</DataBindCateID>
<DataValueField>
Dear customer,
We have temporarily held your order.
Would you please share us why 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>詢問訂單取消原因</DataTextField>
<DataColorField>#000000</DataColorField>
<ParentID>2</ParentID>
</ListItem>
</DataBind>
查詢條件爲ListItemID爲2的修改DataColorField,DataTextField,DataValueField的值
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");