Note that there are some explanatory texts on larger screens.

plurals
  1. POEditing XML node inner text only if has specified value
    text
    copied!<p>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 . Thx</p> <p>I have Xml like this </p> <pre><code>&lt;COREBASE&gt; &lt;AGENT&gt; &lt;AGENT_INDEX&gt;1&lt;/AGENT_INDEX&gt; &lt;AGENT_PORTER_INDEX&gt; &lt;/AGENT_PORTER_INDEX&gt; &lt;AGENT_NAME&gt;John&lt;/AGENT_NAME&gt; &lt;AGENT_SURNAME&gt;Smith&lt;/AGENT_SURNAME&gt; &lt;AGENT_MOBILE_NUMBER&gt;777777777&lt;/AGENT_MOBILE_NUMBER&gt; &lt;/AGENT&gt; &lt;AGENT&gt; &lt;AGENT_INDEX&gt;2&lt;/AGENT_INDEX&gt; &lt;AGENT_PORTER_INDEX&gt;1 &lt;/AGENT_PORTER_INDEX&gt; &lt;AGENT_NAME&gt;Charles&lt;/AGENT_NAME&gt; &lt;AGENT_SURNAME&gt;Bukowski&lt;/AGENT_SURNAME&gt; &lt;AGENT_MOBILE_NUMBER&gt;99999999&lt;/AGENT_MOBILE_NUMBER&gt; &lt;/AGENT&gt; &lt;/COREBASE&gt; </code></pre> <p>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.. :-(</p> <p>Plese i will be glad for any help </p> <pre><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></pre> <p>Am i on the right way but i dont see the doors or i am totaly wrong ? Thx all. Miko</p> <p>OK i tried it this way but it didnt changed the inner text</p> <pre><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 =&gt; a.Element("AGENT_INDEX").Value == agentIndex); xElemAgent.Element("AGENT_MOBILE_NUMBER").Value = textBoxEditAgentMobile.Text; xElemAgent.Element("AGENT_SURNAME").Value = textBoxEditAgentSurname.Text; AgentBaseEdit.Save("AgentBase.xml"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload