Appending to the end of an existing XML

Hi guys,

I'm having a problem updating an existing XML but i'm not getting any errors. The aim of the function is to check if the file exists if it does exist then i want the new values to be appended to the end of the existing XML.

The function checks the existing XMLs without any problems and if it doesn't find the XML that works perfectly aswell but when the file does exist it isnt appending the new information to the end. Please can someone help me

the code below is the part of the function that checks the existing files and attempts to update the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void HashMe::SaveHashButton()
{
	try
	{
		array<String^>^ Available = gcnew array<String^>(100);
		Available = IO::Directory::GetFiles("E:\\XML Folder\\");
		String^ TextBoxText = textBox1->Text->ToString();

		for(int x = 0; x < Available->Length; x++)
		{
			String^ Current = Available[x];
			String^ NoExtension = IO::Path::GetFileNameWithoutExtension(Current);
			
			if(TextBoxText == NoExtension)
			{
				XmlDocument^ XmlDoc = gcnew XmlDocument();
				XmlDoc->Load(Current);

				XmlDoc->CreateElement("Project");
				XmlDoc->CreateAttribute("Name", textBox1->Text->ToString());

				XmlDoc->CreateElement("TestCycle");
				XmlDoc->CreateAttribute("Number", textBox2->Text->ToString());

				for (int x = 0; x < listBox2->Items->Count; ++x)
				{
					String^ FileName = listBox2->Items[x]->ToString();
					String^ Hash = listBox3->Items[x]->ToString();

					XmlDoc->CreateElement("FileName");
					XmlDoc->CreateAttribute("FileName", FileName);

					XmlDoc->CreateElement("HashCode");
					XmlDoc->CreateAttribute("HashCode", Hash);
				}
				XmlDoc->AppendChild(

				XmlDoc->Save(Current);
				return;
			}
		}


The output for creating a brand new XML looks like this:

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<Project Name="d">
  <TestCycle Number="1">
    <FileName>E:\XML Folder\c.xml</FileName>
    <HashCode>FC-8B-4D-EF-F6-21-C8-7A-1B-8C-40-BE-AC-7E-1E-5B</HashCode>
  </TestCycle>
</Project>


So if the update is successful i would like the updated XML to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<Project Name="d">
  <TestCycle Number="1">
    <FileName>E:\XML Folder\c.xml</FileName>
    <HashCode>FC-8B-4D-EF-F6-21-C8-7A-1B-8C-40-BE-AC-7E-1E-5B</HashCode>
  </TestCycle>
</Project>
<Project Name="This is the appended new information">
  <TestCycle Number="99">
    <FileName>E:\XML Folder\c.xml</FileName>
    <HashCode>FC-8B-4D-EF-F6-21-C8-7A-1B-8C-40-BE-AC-7E-1E-5B</HashCode>
    <FileName>E:\XML Folder\c.xml</FileName>
    <HashCode>FC-8B-4D-EF-F6-21-C8-7A-1B-8C-40-BE-AC-7E-1E-5B</HashCode>
    <FileName>E:\XML Folder\c.xml</FileName>
    <HashCode>FC-8B-4D-EF-F6-21-C8-7A-1B-8C-40-BE-AC-7E-1E-5B</HashCode>
    <FileName>E:\XML Folder\c.xml</FileName>
    <HashCode>FC-8B-4D-EF-F6-21-C8-7A-1B-8C-40-BE-AC-7E-1E-5B</HashCode>
  </TestCycle>
</Project>


Please someone help me :(

thanks
...That last XML file needs a root node, just saying...
Topic archived. No new replies allowed.