Search code examples
c#xmlxmldocument

C# WPF Unable to write to XML file


I am new to WPF and am attempting to write a child node to an XML file. Here is the file...

<?xml version="1.0" encoding="utf-8"?>
<Sequences>
  <LastSavedSequence name="Last Saved Sequence">    
     <Test name="Measure Battery Current(Stim)" testNumber="5" Vbat="3.7" Frequency="20" PulseWidth="500" Amplitude="1" Resistance="1600" Anode="1" Cathode="2" ActiveDischarge=""/>
     <Test name="Measure Batther Current(No Stim)" testNumber="6" Vbat="2.9" Frequency="20" PulseWidth="500" Amplitude="1" Resistance="1600" Anode="1" Cathode="2" ActiveDischarge=""/>
  </LastSavedSequence>  
  <ScottTestSequence name="Scott Test Sequence">    
     <Test name="VMO Status" testNumber="4" Vbat="3.7" Frequency="20" PulseWidth="1000" Amplitude="6" Resistance="3000" Anode="1" Cathode="2" ActiveDischarge=""/>
     <Test name="Measure Battery Current(Stim)" testNumber="5" Vbat="3.7" Frequency="20" PulseWidth="500" Amplitude="1" Resistance="1600" Anode="1" Cathode="2" ActiveDischarge=""/>
     <Test name="Measure Batther Current(No Stim)" testNumber="6" Vbat="2.9" Frequency="20" PulseWidth="500" Amplitude="1" Resistance="1600" Anode="1" Cathode="2" ActiveDischarge=""/>
  </ScottTestSequence>  
</Sequences>

I am attempting to create an XML child block to go within . I used stringBuilder and then am trying to do an attach child and then a .save. XMLData2 is a global list and contains a the child elements that I get in the for each. Here is my code...

public static List<System.Xml.XmlNode> xmlData2 = new List<System.Xml.XmlNode>();
XmlDocument xmlFromOutSideSequenceFile = new XmlDocument();
                    xmlFromOutSideSequenceFile.Load("c:\\Users/StarkS02/Documents/SavedSequenceFile.xml");
StringBuilder exampleNode = new StringBuilder();
                        exampleNode.Append("<");
                        exampleNode.Append(tbSequenceName.Text.ToString().Replace(" ", ""));
                        exampleNode.Append(" name=");
                        exampleNode.Append("'");
                        exampleNode.Append(tbSequenceName.Text);
                        exampleNode.Append("'");
                        exampleNode.Append(">");
foreach (XmlNode node in xmlData2)
                        {
XmlElement child = xmlFromOutSideSequenceFile.CreateElement(string.Empty, node.OuterXml, string.Empty);
exampleNode.Append("</");
                        exampleNode.Append(tbSequenceName.Text.ToString().Replace(" ", ""));
                        exampleNode.Append(">");
xmlFromOutSideSequenceFile.AppendChild(exampleNode);
xmlFromOutSideSequenceFile.Save("c:\\Users/StarkS02/Documents/SavedSequenceFile.xml");

I get a compiler error on the .appendChild statement that I cannot convert a stringBuilder to an XML node. This makes sense but I'm not sure how to fix it. Any ideas?


Solution

  • You can create an XML fragment and append to the document.

    var xmlFromOutSideSequenceFile = new XmlDocument();
    xmlFromOutSideSequenceFile.Load("c:\\Users/StarkS02/Documents/SavedSequenceFile.xml");
    

    See here for more on DocumentFragment

    https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createdocumentfragment(v=vs.110).aspx

    var fragment = xmlFromOutSideSequenceFile.CreateDocumentFragment();
    fragment.InnerXml = @"<somexml></somexml>";
    xmlFromOutSideSequenceFile.DocumentElement.FirstChild.AppendChild(fragment);
    

    See here for more on XMLNode

    https://msdn.microsoft.com/en-us/library/system.xml.xmlnode(v=vs.110).aspx

    Decide where you want to put it. .FirstChild .LastChild .NextSibling .ParentNode .PreviousSibling

    Hope this helps! Cheers!