使用LINQ 更新 XML 节点中的信息

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");





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值