开发者

C++/CLI XML creation methods Dll in a WPF form

开发者 https://www.devze.com 2023-03-18 16:01 出处:网络
Update: I have modified the post as per comments. This is going to be a slightly long post i apologize for that, but i want people to understand what im trying to do before shooting it down. I\'ve as

Update: I have modified the post as per comments.

This is going to be a slightly long post i apologize for that, but i want people to understand what im trying to do before shooting it down. I've asked a few question previously about creating methods to create XMLs in C++/CLI and I want to continue using them.

Recently i started testing out C# and WPF and im really enjoying the creativity and freedom it offers for visual effects. I started converting the functions of an application i am currently working on into a DLL mostly so i don't have to redo the work I have already done and because i want to learn and gain some experience in writing DLL's and using interop.

Every function so far has only had a single input or output. However my final two functions (Which take a list of filenames and the hash of the file its self and for each file and hash saves it to an xml file) aren't working so well for me. Im getting output but not in the form i want it so obviously my logic is flawed but im not sure how to correct it.

The functions are very similar the only difference is the one creates a brand new xml while the second updates an existing xml. They both worked perfectly before as straight C++/CLI functions however now that i need to use them in C# i have to put them in iterations so they work for each of the files and their hashes displayed in the lists.

Below is one of the functions in the Dll where i have now replaced the previous hard-coded variables which would have been listBox2->Items[x]->ToString() as varibales such as CurrentFile which are now defined in the .cs.

public: void XMLUpdate(String^ Location, int NumberItems, String^ ProjectName, 
                String^ ProjectTC, String^ CurrentFile, String^ CurrentHash)
    {
        try
        {
            XmlDocument^ XmlDoc = gcnew XmlDocument();
            XmlDoc->Load(Location);

            XmlElement^ NewProject = XmlDoc->CreateElement("Project");
            NewProject->SetAttribute("Name", ProjectName);
            XmlDoc->DocumentElement->AppendChild(NewProject);

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

            XmlElement^ NewFile = XmlDoc->CreateElement("Files");
            NewTestCycle->AppendChild(NewFile);

            for (int x = 0; x < NumberItems; ++x)
            {
                String^ FileName = CurrentFile;
                String^ Hash = CurrentHash;

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

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

            XmlDoc->Save(Location);
            return;
        }
        catch(Exception^)
        {
            return;
        }           
     }

Here is the call for the method from the dll in my WPF form:

        private void button2_Click(object sender, RoutedEventArgs e)
    {
        DllTest.Funtions Functions = new DllTest.Funtions();

        String Name = textBox1.Text;
        String TC = textBox2.Text;
        String Location = "C:\\Users\\brandonm\\Desktop\\Backup\\XML\\test1.xml";
        int Number = listBox2.Items.Count;

        for (int x = 0; x < listBox2.Items.Count; ++x)
        {
            Functions.XMLUpdate(Location, Number, TC, Name, listBox2.Items[x].ToString(),
                    listBox3.Items[x].ToString());
        }

    }

Output is only taking the last item in the list and duplicating it by the number in the list in this case the list had 3 filenames and the second list had 3 hashes here is an example:

<?xml version="1.0" encoding="utf-8"?>
<Project Name="New">
  <TestCycle Number="1">
  <Files>
    <FileName
      File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
    <HashCode
      Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
    <FileName
      File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
    <HashCode
      Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
    <FileName
      File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
    <HashCode
      Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
  </Files>
    </TestCycle>
  </Project>
</Project>

So here is the second function that updates an exisitng XML and gives me completely different output, still incorrect, from the output in the new function.

public: void XMLUpdate(String^ Location, int NumberItems, String^ ProjectName, 
                String^ ProjectTC, String^ CurrentFile, String^ CurrentHash)
    {
        try
        {
            XmlDocument^ XmlDoc = gcnew XmlDocument();
            XmlDoc->Load(Location);

            XmlElement^ NewProject = XmlDoc->CreateElement("Project");
            NewProject->SetAttribute("Name", ProjectName);
            XmlDoc->DocumentElement->AppendChild(NewProject);

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

            XmlElement^ NewFile = XmlDoc->CreateElement("Files");
            NewTestCycle->AppendChild(NewFile);

            for (int x = 0; x < NumberItems; ++x)
            {
                String^ FileName = CurrentFile;
                String^ Hash = CurrentHash;

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

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

            XmlDoc->Save(Location);
            return;
        }
        catch(Exception^)
        {
            return;
        }           
     }

The method call:

private void button4_Click(object sender, RoutedEventArgs e)
    {
        DllTest.Funtions Functions = new DllTest.Funtions();

        String Name = textBox1.Text;
        String TC = textBox2.Text;
        String path = "C:\\Users\\brandonm\\Desktop\\Backup\\XML\\test1.xml";
        int Number = listBox2.Items.Count;

        for (int x = 0; x < listBox2.Items.Count; ++x)
        {
            Functions.XMLNew(path, Name, TC, Number, listBox2.Items[x].ToString(),
                listBox3.Items[x].ToString());
        }
    }

the output for this one which is closer to what i need since it using all of the items in the list but its duplicating them in every element i only want a single new element with the new files and their hashes.

    <?xml version="1.0" encoding="utf-8"?>
    <Project Name="New">
    <TestCycle Number="1">
      <Files>
        <Fi开发者_JAVA技巧leName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
        <HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
        <HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
        <HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
      </Files>
    </TestCycle>
  </Project>
  <Project Name="2">
    <TestCycle Number="New">
      <Files>
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\DllTest.dll" />
        <HashCode Code="0E-C2-B1-A4-3C-D8-C0-51-96-0F-FF-19-BC-3A-CE-AC" />
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\DllTest.dll" />
        <HashCode Code="0E-C2-B1-A4-3C-D8-C0-51-96-0F-FF-19-BC-3A-CE-AC" />
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\DllTest.dll" />
        <HashCode Code="0E-C2-B1-A4-3C-D8-C0-51-96-0F-FF-19-BC-3A-CE-AC" />
      </Files>
    </TestCycle>
  </Project>
  <Project Name="2">
    <TestCycle Number="New">
      <Files>
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.exe" />
        <HashCode Code="C0-7C-51-C2-92-70-1B-11-E0-26-6D-D5-B8-79-12-0D" />
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.exe" />
        <HashCode Code="C0-7C-51-C2-92-70-1B-11-E0-26-6D-D5-B8-79-12-0D" />
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.exe" />
        <HashCode Code="C0-7C-51-C2-92-70-1B-11-E0-26-6D-D5-B8-79-12-0D" />
      </Files>
    </TestCycle>
  </Project>
  <Project Name="2">
    <TestCycle Number="New">
      <Files>
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
        <HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
        <HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
        <FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
        <HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
      </Files>
    </TestCycle>
  </Project>
    </Project>

So now you can see the problem i have loops within loops causing output that repeats its self. I can post an example of my output if you need.

But for the life of me i cannot get around it, i have tried splitting up the functions in to parts but i get errors trying to do that.

Can anyone see a way around this? I only want to completely rewrite the methods in c# if its my only alternative since i want all the main functions in the dll. Please dont bash me for my lack of knowledge im still learning and testing alot of things for the first time.

Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消