I am new to c# and I am working with a xml file. I have appended to an existing sample file, but the formatting is off and some of the xml elements are missing. What I am working toward is appending to it 10 times just changing the number for the element D100 but keep the same data I have just appened for D100 number 2.
Here is the sample xml file I am using
<flp:Tab xmlns:flp="http://www.w3.org/2001/flp" 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>
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 AppendX
{
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");
XmlNamespaceManager namespaces = new XmlNamespaceManager(doc.NameTable);
namespaces.AddNamespace("flp", "http://www.w3.org/2001/flp");
XmlNode nextNode = doc.SelectSingleNode("/flp:Tab/flp:Designs", namespaces);
XmlElement D100 = doc.CreateElement("flp", "D100", "http://www.w3.org/2001/flp");
D100.SetAttribute("Number", "2");
XmlElement Code = doc.CreateElement("flp", "Code", "http://www.w3.org/2001/flp");
Code.InnerText = "B";
D100.AppendChild(Code);
XmlElement Documented = doc.CreateElement("flp", "Documented", "http://www.w3.org/2001/flp");
Documented.InnerText = "false";
D100.AppendChild(Documented);
XmlElement Note = doc.CreateElement("flp", "Note", "http://www.w3.org/2001/flp");
Note.InnerText = "Complete";
D100.AppendChild(Note);
XmlElement Tester = doc.CreateElement("flp", "Tester", "http://www.w3.org/2001/flp");
Tester.SetAttribute("Name", "John");
D100.AppendChild(Tester);
XmlElement Title = doc.CreateElement("flp", "Title", "http://www.w3.org/2001/flp");
Title.SetAttribute("Number", "0");
Title.SetAttribute("Name", "Ronald");
D100.AppendChild(Title);
XmlElement Start = doc.CreateElement("flp", "Start", "http://www.w3.org/2001/flp");
Start.InnerText = "Begin";
D100.AppendChild(Start);
XmlElement Finish = doc.CreateElement("flp", "Finish", "http://www.w3.org/2001/flp");
Finish.InnerText = "End";
D100.AppendChild(Finish);
XmlElement TestGiver = doc.CreateElement("flp", "TestGiver", "http://www.w3.org/2001/flp");
TestGiver.SetAttribute("Name", "Jeremy");
D100.AppendChild(TestGiver);
XmlElement IsRequired = doc.CreateElement("flp", "IsRequired", "http://www.w3.org/2001/flp");
IsRequired.InnerText = "true";
D100.AppendChild(IsRequired);
XmlElement IsOptional = doc.CreateElement("flp", "IsOptional", "http://www.w3.org/2001/flp");
IsOptional.InnerText = "false";
D100.AppendChild(IsOptional);
nextNode.AppendChild(D100);
doc.Save("test2.xml");
}
}
}
Here is the new xml file
<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:D100 Number="2">
<flp:Code>B</flp:Code>
<flp:Documented>false</flp:Documented>
<flp:Note>Complete</flp:Note>
<flp:Tester Name="John" />
<flp:Title Number="0" Name="Ronald" />
<flp:Start>Begin</flp:Start>
<flp:Finish>End</flp:Finish>
<flp:TestGiver Name="Jeremy" />
<flp:IsRequired>true</flp:IsRequired>
<flp:IsOptional>false</flp:IsOptional>
</flp:D100>
</flp:Designs>
</flp:Tab>
As you can see the formatting is different after appending the new data. I am also missing some elements like Titles and the closing tags are different for some like Title. How can I preserve the formatting form the original document. Thanks!