[C#] Append XML string block to existing XmlDocument


Answers

Try this:

employeeNode.InnerXml = "<Demographic><Age/><DOB/></Demographic>";

Alternatively (if you have another XML document that you want to use):

employeeNode.AppendChild(employeeNode.OwnerDocument.ImportNode(otherXmlDocument.DocumentElement, true));
Question

I have an XmlDocument that already exists and is read from a file.

I would like to add a chunk of Xml to a node in the document. Is there a good way to create and add all the nodes without cluttering my code with many .CreateNote and .AppendChild calls?

I would like some way of making a string or stringBuilder of a valid Xml section and just appending that to an XmlNode.

ex: Original XmlDoc:

<MyXml>
   <Employee>
   </Employee>
</MyXml>

and, I would like to add a Demographic (with several children) tag to Employee:

<MyXml>
   <Employee>
      <Demographic>
         <Age/>
         <DOB/>
      </Demographic>
   </Employee>
</MyXml>



Using XML to Store Colours and Strings in Unity

So I managed to wrap my head around where I wanted things to go, thanks to a few more days of research (and especially this link).

I'm answering my own question so it may help someone else who has the same problem (and because I guess it's a bit silly to leave it unanswered now I have the solution).

To get around the issue of overwriting things, I used the method shown in the link, making an if loop to check whether the file existed. In the first section (the case where the file does not already exist), I used xmlWriter to put things together. XmlWriter appears to be a lot simpler than the method I use in the other section, with the downside that you don't seem to be able to use it without overwriting everything (hence only using it when the file doesn't already exist).

In the case that the file does exist, xDocument is brilliant. I modified the code from the link so that it replaced elements rather than adding new ones (although that can still be done). For this to work, include system.linq and system.xml (possibly system.xml.linq as well). Here is an example:

   // Loads the document
   XDocument xDocument = XDocument.Load("document.xml");
            // Specifies the encoding
            new XDeclaration("1.0","utf-8","yes");
            // Follows the route settings-font and replaces the current colour 
            // element with a new one
            xDocument.Element("Settings").Element("Font").Element("Colour").ReplaceWith(new XElement("Colour",fontColourHex));
           // As above
            xDocument.Element("Settings").Element("Background").Element("Colour").ReplaceWith(new XElement("Colour",backgroundColourHex));

            // One of the more important things to note is that everything saved in an 
            // element ought to be a string, and the xml file doesn't like element 
            // names with spaces in them (use an underscore instead)
            xDocument.Element("Settings").Element("Font").Element("Size_Modifier").ReplaceWith(new XElement("Size_Modifier",globalFontSizeModifier.ToString()));

            // Saves the document
            xDocument.Save("document.xml");

If you're wondering why there is no whitespace between words in this code, it's because, for some reason, Mono doesn't like it. I did find a fix somewhere else for it, but I couldn't get it to work and deleting the whitespace does the job fine.

The resultant file looked like this:

<?xml version="1.0" encoding="utf-8"?>
<Settings>
  <Font>
    <Colour>FFFFFFFF</Colour>
    <Size_Modifier>0</Size_Modifier>
  </Font>
  <Background>
    <Colour>000000FF</Colour>
  </Background>
</Settings>

After I modified it and tied it to a couple of functions, all I had to do was create a new game object with the script attached and use it as needed.




Insert a RootNode in an XML using C# XmlDocument

maybe something like that ?

XmlDocument xml = new XmlDocument();
string nodesString = "<node/>" ;
xml.LoadXml("<root>" + nodesString+ "</root>");