Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It does not work, because you are selecting the first agent explicitly with in each loop</p> <pre><code>AgentBaseEdit.SelectSingleNode("COREBASE/AGENT/...") </code></pre> <hr> <p>But you can do it easier by reading and changing withing the same xml document. I'm only changing the agent name and replacing it with "test 1", "test 2", ...</p> <pre><code>XDocument AgentBase = XDocument.Load("AgentBase.xml"); int i = 0; foreach (XElement el in AgentBase.Descendants("AGENT")) { el.Element("AGENT_NAME").Value = "test " + ++i; // ... } AgentBase.Save("AgentBase.xml"); </code></pre> <hr> <p><strong>UPDATE</strong></p> <p>However, I'm suggesting you to separate the logic involving the XML handling from the form. Start by creating an Agent class</p> <pre><code>public class Agent { public string Index { get; set; } public string PorterIndex { get; set; } public string Name { get; set; } public string Surname { get; set; } public string Mobile { get; set; } } </code></pre> <p>Then create an interface defining the needed functionality for an agent repository. The advantage of this interface is that it will make it easier later to switch to another kind of repository like a relational database.</p> <pre><code>public interface IAgentRepository { IList&lt;Agent&gt; LoadAgents(); void Save(IEnumerable&lt;Agent&gt; agents); } </code></pre> <p>Then create a class that handles the agents. Here is a suggestion:</p> <pre><code>public class AgentXmlRepository : IAgentRepository { private string _xmlAgentsFile; public AgentXmlRepository(string xmlAgentsFile) { _xmlAgentsFile = xmlAgentsFile; } public IList&lt;Agent&gt; LoadAgents() { XDocument AgentBase = XDocument.Load(_xmlAgentsFile); var agents = new List&lt;Agent&gt;(); foreach (XElement el in AgentBase.Descendants("AGENT")) { var agent = new Agent { Index = el.Element("AGENT_INDEX").Value, PorterIndex = el.Element("AGENT_PORTER_INDEX").Value, Name = el.Element("AGENT_NAME").Value, Surname = el.Element("AGENT_SURNAME").Value, Mobile = el.Element("AGENT_MOBILE_NUMBER").Value }; agents.Add(agent); } return agents; } public void Save(IEnumerable&lt;Agent&gt; agents) { var xDocument = new XDocument( new XDeclaration("1.0", "utf-8", null), new XElement("COREBASE", agents.Select(a =&gt; new XElement("AGENT", new XElement("AGENT_INDEX", a.Index), new XElement("AGENT_PORTER_INDEX", a.PorterIndex), new XElement("AGENT_NAME", a.Name), new XElement("AGENT_SURNAME", a.Surname), new XElement("AGENT_MOBILE_NUMBER", a.Mobile) ) ) ) ); xDocument.Save(_xmlAgentsFile); } } </code></pre> <p>The form can now concentrate on the editing logic. The form does not even need to know what kind of repository to use if you inject the repository in the form constructor (of cause the form constructor must declare a parameter of type <code>IAgentRepository</code>):</p> <pre><code>var myAgentForm = new AgentForm(new AgentXmlRepository("AgentBase.xml")); myAgentForm.Show(); </code></pre> <hr> <p><strong>UPDATE #2</strong></p> <p>Note that you cannot change a single item within an XML file. You must load all the agents, make an edit and then rewrite the whole file, even if you edited only one agent.</p> <p>To do this, you can use my <code>LoadAgents</code> method, then pick an agent from the returned list, edit the agent and finally write the agents list back to the file with my <code>Save</code> method. You can find an agent in the list with LINQ:</p> <pre><code>Agent a = agents .Where(a =&gt; a.Index == x) .FirstOrDefault(); </code></pre> <p>This returns <code>null</code> if an agent with the required index does not exist. Since <code>Agent</code> is a reference type, you don’t have to write it back to the list. The list is keeping a reference to the same agent as the variable <code>a</code>.</p>
 

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