更新/附加到现有XML

时间:2011-04-03 09:07:12

标签: c# xml c++-cli append

我在更新现有XML时遇到问题,但我没有收到任何错误。该函数的目的是检查文件是否存在(如果它存在)然后我希望将新值附加到现有XML的末尾。

该函数检查现有的XML没有任何问题,如果它找不到完美的XML,但是当文件存在时,它不会将新信息附加到最后。请有人帮帮我

以下代码是检查现有文件并尝试更新文件的函数的一部分。

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

创建全新XML的输出如下所示:

<?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>

因此,如果更新成功,我希望更新的XML看起来像这样:

<?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>

请有人帮帮我:(

1 个答案:

答案 0 :(得分:0)

以下代码适用于我:

XmlDocument^ XmlDoc = gcnew XmlDocument();
XmlDoc->Load("projects.xml");

XmlElement^ NewProject = XmlDoc->CreateElement("Project");
NewProject->SetAttribute("Name", "This is the appended new information");
XmlDoc->DocumentElement->AppendChild(NewProject);

XmlElement^ NewTestCycle = XmlDoc->CreateElement("TestCycle");
NewProject->SetAttribute("Number", "99");
NewProject->AppendChild(NewTestCycle);

for (int x = 0; x < 4; ++x)
{
    String^ FileName = "E:\\XML Folder\\c.xml";
    String^ Hash = "FC-8B-4D-EF-F6-21-C8-7A-1B-8C-40-BE-AC-7E-1E-5B";

    XmlElement^ NewFileName = XmlDoc->CreateElement("FileName");
    NewFileName->SetAttribute("FileName", FileName);
    NewTestCycle->AppendChild(NewFileName);

    XmlElement^ NewHashCode = XmlDoc->CreateElement("HashCode");
    NewHashCode->SetAttribute("HashCode", Hash);
    NewTestCycle->AppendChild(NewHashCode);
}

XmlDoc->Save("new projects.xml");

此外,如果我正确理解XML,更好的结构将类似于以下内容,因此显而易见<FileName><HashCode>属于一起。

<File>
  <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>
</File>