使用LINQ 更新 XML 節點中的資訊

來源:互聯網
上載者:User

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





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.