Hi all,

I'm having an issue with updating an XML that already exists. I have no problem creating an XML but when it comes appending to the end an existing xml nothing happens.

I dont get any error messages so I'm not sure what I'm missing, the first part where it checks if the file exists seems to work fine. I have a default folder where XMLs are saved and if the XML exists i just want to update the end of it with the new information. The code below is what I'm using to do this function.

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);
				}
				return;
			}
		}

The output of my XMLs looks like the code below when it gets created for the first time so I would like the XML to look the same after being updated:

<?xml version="1.0" encoding="utf-8"?>
<Project
  Name="b">
  <TestCycle
    Number="1">
    <FileName>E:\XML Folder\images.jpeg</FileName>
    <HashCode>44-61-8F-3B-29-1A-CC-2D-15-A8-6A-AF-FF-72-20-72</HashCode>
  </TestCycle>
</Project>

So if the function works it should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<Project
  Name="b">
  <TestCycle
    Number="1">
    <FileName>E:\XML Folder\images.jpeg</FileName>
    <HashCode>44-61-8F-3B-29-1A-CC-2D-15-A8-6A-AF-FF-72-20-72</HashCode>
  </TestCycle>
<Project
  Name="b">
  <TestCycle
    Number="999">
    <FileName>E:\XML Folder\images.jpeg</FileName>
    <HashCode>44-61-8F-3B-29-1A-CC-2D-15-A8-6A-AF-FF-72-20-72</HashCode>
    <FileName>E:\XML Folder\images.jpeg</FileName>
    <HashCode>44-61-8F-3B-29-1A-CC-2D-15-A8-6A-AF-FF-72-20-72</HashCode>
  </TestCycle>
</Project>

Any help would be greatly appreciated :)

Thanks

I'm using

namespace System::XML

to create/update XML files

Edited 5 Years Ago by brandonm: n/a

This article has been dead for over six months. Start a new discussion instead.