Editing XML node inner text only if has specified value

Igorlbg

New Member
Hello i need your super help.Im not soo skilled in C# and i stack for about 6 hours on this. So please if anyone know help me . ThxI have Xml like this \[code\]<COREBASE> <AGENT> <AGENT_INDEX>1</AGENT_INDEX> <AGENT_PORTER_INDEX> </AGENT_PORTER_INDEX> <AGENT_NAME>John</AGENT_NAME> <AGENT_SURNAME>Smith</AGENT_SURNAME> <AGENT_MOBILE_NUMBER>777777777</AGENT_MOBILE_NUMBER> </AGENT> <AGENT> <AGENT_INDEX>2</AGENT_INDEX> <AGENT_PORTER_INDEX>1 </AGENT_PORTER_INDEX> <AGENT_NAME>Charles</AGENT_NAME> <AGENT_SURNAME>Bukowski</AGENT_SURNAME> <AGENT_MOBILE_NUMBER>99999999</AGENT_MOBILE_NUMBER> </AGENT></COREBASE>\[/code\]And I need to select agent by index in windows forms combo box and than edit and save his attributes to xml. I found how to edit and save it but i dont know why but its saved to the first agent and overwrite his attributes in XML but not in the selected one.. :-(Plese i will be glad for any help \[code\]private void buttonEditAgent_Click(object sender, EventArgs e){ XmlDocument AgentBaseEdit = new XmlDocument(); AgentBaseEdit.Load("AgentBase.xml"); XDocument AgentBase = XDocument.Load("AgentBase.xml"); var all = from a in AgentBase.Descendants("AGENT") select new { agentI = a.Element("AGENT_INDEX").Value, porterI = a.Element("AGENT_PORTER_INDEX").Value, agentN = a.Element("AGENT_NAME").Value, agentS = a.Element("AGENT_SURNAME").Value, agentM = a.Element("AGENT_MOBILE_NUMBER").Value, }; foreach (var a in all) { if ("" == textBoxEditAgentIndex.Text.ToString()) { MessageBox.Show("You must fill Agent Index field !!", "WARNING"); } else { // AgentBaseEdit.SelectSingleNode("COREBASE/AGENT/AGENT_INDEX").InnerText == textBoxEditAgentIndex.Text if (a.agentI == textBoxEditAgentIndex.Text.ToString()) { AgentBaseEdit.SelectSingleNode("COREBASE/AGENT/AGENT_INDEX").InnerText = textBoxEditAgentIndex.Text; AgentBaseEdit.SelectSingleNode("COREBASE/AGENT/AGENT_PORTER_INDEX").InnerText = textBoxEditAgentPorterIndex.Text; AgentBaseEdit.SelectSingleNode("COREBASE/AGENT/AGENT_NAME").InnerText = textBoxEditAgentName.Text; AgentBaseEdit.SelectSingleNode("COREBASE/AGENT/AGENT_SURNAME").InnerText = textBoxEditAgentSurname.Text; AgentBaseEdit.SelectSingleNode("COREBASE/AGENT/AGENT_MOBILE_NUMBER").InnerText = textBoxEditAgentMobile.Text; AgentBaseEdit.Save("AgentBase.xml"); ClearEditAgentTxtBoxes(); } } }} \[/code\]Am i on the right way but i dont see the doors or i am totaly wrong ? Thx all. MikoOK i tried it this way but it didnt changed the inner text\[code\]string agentIndex = comboBoxEditAgentI.SelectedItem.ToString(); XmlDocument AgentBaseEdit = new XmlDocument(); AgentBaseEdit.Load("AgentBase.xml"); XDocument AgentBase = XDocument.Load("AgentBase.xml"); var xElemAgent = AgentBase.Descendants("AGENT") .First(a => a.Element("AGENT_INDEX").Value =http://stackoverflow.com/questions/14084521/= agentIndex); xElemAgent.Element("AGENT_MOBILE_NUMBER").Value = http://stackoverflow.com/questions/14084521/textBoxEditAgentMobile.Text; xElemAgent.Element("AGENT_SURNAME").Value = http://stackoverflow.com/questions/14084521/textBoxEditAgentSurname.Text; AgentBaseEdit.Save("AgentBase.xml");\[/code\]
 
Back
Top