Home > OS >  C# Add an element between value of an element
C# Add an element between value of an element

Time:10-05

I have created a XML file with the XmlSerializer. I need to insert a certain XMl element in an existing element, but I need to insert it between the value of this element. For example:

Before

<Root>
   <Child1>Test_Test_Test</Child1>
</Root>

Needed

<Root>
   <Child1>Test_<InsertElement>Test</InsertElement>_Test</Child1>
</Root>

I have tried 2 ways. 1 where I get the value of Child1 and add the xml as a string (won't work because XML doesn't accept "<, >, & etc.) and 2 with loading the created XML file, get the Child1 element and add the wanted element. This will cause the to be at the end of the Child1 element.

CodePudding user response:

Implement your own xml serializer, which knows how to insert those values what you want. Or use XmlSerializer and insert a unique text where you want your special element and replace in the xml file manually (with regex).

CodePudding user response:

u can add your specified node to desire node of your tree with XmlDocument follow this example :

XmlDocument doc = new XmlDocument();
        doc.LoadXml("<Root>"  
                    "<TEST></TEST>"  
                    "<Child1>TEXT</Child1>"  
                    "</Root>");

        XmlNode root = doc.SelectSingleNode("//Child1");

        //Create a new nodes.
        XmlElement elem = doc.CreateElement("child2");
        elem.InnerText = "text2";
        XmlElement elemChild = doc.CreateElement("child3");
        elemChild.InnerText = "text32";

        //Add the node to the document.
        elem.AppendChild(elemChild);
        root.AppendChild(elem.Clone());

CodePudding user response:

With some regex to split the string you can assemble the new contents of the <Child1> node using XElement.ReplaceAll:

using System.Xml.Linq;
using System.Xml.XPath;
using System.Text.RegularExpressions;

var before = @"
<Root>
    <Child1>Test_Test_Test</Child1>
</Root>";

var doc = XElement.Parse(before);
var child = doc.XPathSelectElement("/Child1");

// Split your string with whatever logic you need:
var matches = Regex.Match(child.Value, @"(\w _)(\w )(_\w )");
// Create the new element with the "Test" content
var newElem = new XElement("InsertElement", matches.Groups[2].Value);
// Replace the contents of Child1 with the new element
child.ReplaceAll(
    matches.Groups[1],  // "Test_"
    newElem,            // "<InsertElement>Test</InsertElement>"
    matches.Groups[3]); // "_Test"

Console.WriteLine(doc);
// <Root>
//   <Child1>Test_<InsertElement>Test</InsertElement>_Test</Child1>
// </Root>

Check this fiddle for an example.

CodePudding user response:

It is not going to work because <Child1>Test_<InsertElement>Test</InsertElement>_Test</Child1 will not be a valid XML.

A dirty trick you can do is to consider <Root><Child1>Test_Test_Test</Child1></Root> as a string and do a replace on _Test_ with _<InsertElement>Test</InsertElement>_. Or to put any other kind of unique marker inside to be easy to find and replace with the wanted xml/string.

  • Related