如何使用linq更新xml节点 [英] how to update xml node with linq

查看:91
本文介绍了如何使用linq更新xml节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai朋友我需要更新一个带有节点属性的xml文件我的xml结构是

hai friends i need to update an xml file with node attributes my xml structure is

<BankName BankName="jinesh" TemplateModel="sam">
<ChqBasics>
  <ChqL>30</ChqL>
  <ChqW>50</ChqW>
  <ChqImgPath>hai</ChqImgPath>
</ChqBasics>
<XandYPosition>
  <column Name="ChqDate" X="10" Y="10" />
  <column Name="CPayAgainst" X="10" Y="10" />
  <column Name="ChqAmtDgt" X="10" Y="10" />
  <column Name="ChqAmtWrds" X="10" Y="10" />
</XandYPosition>





i需要更新XandYPosition节点中的元素,所以我为此创建了一个函数但是我无法完成此。请帮助我实现这个目标





i need to update the elements in the XandYPosition node so i created a function for this but i am not able to complete this .please help me to achieve this

public static void updatenode(string bankname, string templatemodel,string field,string x,string y)
    {
        XDocument doc = XDocument.Load("newtest.xml");
       var  updatenode = doc
                    .Descendants("BankName")
                    .Where(item => item.Attribute("BankName").Value == bankname && item.Attribute("TemplateModel").Value == templatemodel)
                    .Select(XandYPosition => XandYPosition.Descendants("XandYPosition").Descendants());
    }



函数调用将是这样的


function call will be like this

BasicClass.updatenode(comboBox1.Text, comboBox2.Text, "ChqDate", "1000", "2000");

推荐答案

//yor localhost is related to your application
var xelement = XElement.Load("http://localhost:1694/newtest.xml");

var query = (from m in xelement.Descendants("BankName")
             where m.Attribute("BankName").Value == "jinesh" && m.Attribute("TemplateModel").Value == "sam"
             from b in m.Element("XandYPosition").Elements("column")
             where b.Attribute("Name").Value == "ChqDate"
             select b).FirstOrDefault();


if (query != null)
{
       query.Attribute("X").SetValue("1000");
       query.Attribute("Y").SetValue("2000");

}

//Combine by foreach from xelement+queryresult --> new xml (newtest) and newtest.Save("..\\..\\newtest.xml");


看看这里:

http: //stackoverflow.com/questions/10800266/updating-xml-node-attributes-using-linq [ ^ ]

http://stackoverflow.com/questions/13342172/updating-xml-attribute-in-subtree-using-linq-to-xml [ ^ ]

http://forums.asp.net/t/1689260.aspx?Query+for+upd ate + attribute + value + using + linq + to + xml + [ ^ ]
Have a look here:
http://stackoverflow.com/questions/10800266/updating-xml-node-attributes-using-linq[^]
http://stackoverflow.com/questions/13342172/updating-xml-attribute-in-subtree-using-linq-to-xml[^]
http://forums.asp.net/t/1689260.aspx?Query+for+update+attribute+value+using+linq+to+xml+[^]


这篇关于如何使用linq更新xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆