Home > database >  C# XML - Modify a line of a node
C# XML - Modify a line of a node

Time:07-25

I have for example the following XML-File. As clean identifier exists "EA MagicNo" with a single id that will never be used twice.

In this example you can see, that the "Symbol" behind "EA MagicNo=111" is empty and must get a value e.g. "BBB".

<?xml version="1.0" encoding="utf-8"?>
<EAs>
  <EA MagicNo="000">
    <Date>2022-07-21</Date>
    <Symbol>AAA</Symbol>
  </EA>
  <EA MagicNo="111">
    <Date>2022-07-22</Date>
    <Symbol></Symbol>
  </EA>
</EAs>

I have a Forms-Application and use Buttons for doing everything.

So I can remove a complete Node with this code:

private void Btn_Remove_Click(object sender, EventArgs e)
{
    // load xml
    XDocument doc = XDocument.Load(@"test.xml");

    // remove a complete EA-Magic Element-Node
    XElement xel = doc.Descendants("EA")
        .Where(e => (string)e.Attribute("MagicNo") == "000")
        .SingleOrDefault();

    if (xel != null)
    {
        xel.Remove();
    }

    // save xml
    doc.Save(@"test.xml");
}

In result the XML looks after this like:

<?xml version="1.0" encoding="utf-8"?>
<EAs>
  <EA MagicNo="111">
    <Date>2022-07-22</Date>
    <Symbol></Symbol>
  </EA>
</EAs>

Also I can add a complete new Node with this Code:

private void Btn_Add_Click(object sender, EventArgs e)
{
    // load xml
    XDocument doc = XDocument.Load(@"test.xml");

    // add a new "EA" to "EAs"
    var parent = doc.Descendants("EAs")
        .SingleOrDefault();

    if (parent != null)
    {
        // create a new EA Node
        XElement el =
            new XElement("EA",
                new XAttribute("MagicNo", "222"),
                new XElement("Date", "2022-07-23"),
                new XElement("Symbol", "CCC")
            );

        // add it
        parent.Add(el);
}

But I am struggling to modify one line of an existing Node to add the empty Value "BBB". Please can you help me out with the needed lines of code?

Prepared code to start with:

private void Btn_Modify_Click(object sender, EventArgs e)
{
    // load xml
    XDocument doc = XDocument.Load(@"test.xml");

    // Code to modify the XElement here ...


    // save xml
    doc.Save(@"test.xml");
}

CodePudding user response:

Seems like you can just use Linq to get the empty nodes, then modify the Value of each

var nodes = doc.Elements("EAs")
    .SelectMany(e => e.Elements("EA"))
    .SelectMany(e => e.Elements("Symbol"))
    .Where(e => e.Value == "");

foreach (var node in nodes)
{
    node.Value = "BBB";
}

dotnetfiddle

CodePudding user response:

Working solution:

private void Btn_Modify_Click(object sender, EventArgs e)
{
    // load xml
    XDocument doc = XDocument.Load(@"test.xml");

    // Code to modify the XElement here ...
    // identify exact Node
    XElement xel = doc.Descendants("EA").Where(x => (string)x.Attribute("MagicNo") == "111").Single();
    // modify Value
    xel.Element("Symbol").Value = "BBB";

    // save xml
    doc.Save(@"test.xml");
}

  • Related