DewSql
DewSql

Reputation: 163

Append to existing Xml in C#

I have a Xml File below that I am trying to load in to visual studio then append another entry for D100 to the file then write or append it 1000 times.

The code below saves the document but nothing gets appended.

<flp:Tab xmlns:flp="http://www.w3.org/2001/XMLSchema"   Title="Testing">
  <flp:Form Number="0" id="1005" />
  <flp:Rev Time="2013-01-21T15:08:00">
    <flp:Author Name="Brad" Aid="15" />
  </flp:Rev>
  <flp:Designs Id="D100">
    <flp:D100 Number="1">
      <flp:Code>A</flp:Code>
      <flp:Documented>true</flp:Documented>
      <flp:Note>In Process</flp:Note>
      <flp:Testers>
        <flp:Tester Name="David">
          <flp:Titles>
            <flp:Title Number="0" Name="Entry 1">
              <flp:Start>Begin</flp:Start>
              <flp:Finish>End</flp:Finish>
            </flp:Title>
          </flp:Titles>
        </flp:Tester>
      </flp:Testers>
      <flp:TestGivers>
        <flp:TestGiver Name="James" />
      </flp:TestGivers>
      <flp:IsRequired>true</flp:IsRequired>
      <flp:IsOptional>false</flp:IsOptional>
    </flp:D100>
  </flp:Designs>
</flp:Tab>

I am trying to append and write out the information 1000 times in the Xml File

Here is my C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;


namespace AppendXml
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\Desktop\\Temp.xml");

            //XmlElement root = doc.CreateElement("Tab");

            XmlElement D100 = doc.CreateElement("D100");
            D100.SetAttribute("Number", "2");

            XmlElement Code = doc.CreateElement("Code");
            Code.InnerText = "B";

            XmlElement Documented = doc.CreateElement("Documented");
            Documented.InnerText = "false";

            XmlElement Note = doc.CreateElement("Note");
            Note.InnerText = "Complete";

            XmlElement Tester = doc.CreateElement("Tester");
            Tester.SetAttribute("Name", "John");

            XmlElement Title = doc.CreateElement("Title");
            Title.SetAttribute("Number", "0");
            Title.SetAttribute("Name", "Ronald");

            XmlElement Start = doc.CreateElement("Start");
            Start.InnerText = "Begin";

            XmlElement Finish = doc.CreateElement("Finish");
            Finish.InnerText = "End";

            XmlElement TestGiver = doc.CreateElement("TestGiver");
            TestGiver.SetAttribute("Name", "Jeremy");

            XmlElement IsRequired = doc.CreateElement("IsRequired");
            IsRequired.InnerText = "true";

            XmlElement IsOptional = doc.CreateElement("IsOptional");
            IsOptional.InnerText = "false";




            D100.AppendChild(IsOptional);
            D100.AppendChild(IsRequired);
            D100.AppendChild(TestGiver);
            D100.AppendChild(Finish);
            D100.AppendChild(Start);
            D100.AppendChild(Title);
            D100.AppendChild(Tester);
            D100.AppendChild(Note);
            D100.AppendChild(Documented);
            D100.AppendChild(Code);

            //root.AppendChild(D100);
            //doc.AppendChild(root);

            doc.Save("test13.xml");



        }
      }
    }

The document saves but noting appends. What am I leaving out?

Upvotes: 0

Views: 2090

Answers (1)

MiMo
MiMo

Reputation: 11983

You should append D100 to flp:Designs - now you are not appending it to anything, hence nothing gets added to the document:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("flp", "http://www.w3.org/2001/XMLSchema");
XmlNode designs = doc.SelectSingleNode("//flp:Designs", nsmgr);
designs.AppendChild(D100);

You are also creating D100 in the default namespace, probably you want to create it in the http://www.w3.org/2001/XMLSchema namespace with flp prefix as the rest of the XML:

XmlElement D100 = doc.CreateElement("flp", "D100", "http://www.w3.org/2001/XMLSchema");

Lastly: http://www.w3.org/2001/XMLSchema is a standard namespace, that here is used for some custom XML, this is in general wrong.

Upvotes: 1

Related Questions