Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hai friends i need to update an xml file with node attributes my xml structure is
XML
<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 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

C#
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
C#
BasicClass.updatenode(comboBox1.Text, comboBox2.Text, "ChqDate", "1000", "2000");
Posted

C#
//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");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900