C# C++/用于WPF表单的Dll中的CLI XML方法
很抱歉发了这么长的帖子,输出量很大。最近我开始测试C#和WPF,我真的很享受它为视觉效果提供的创造力和自由。我开始将当前正在处理的应用程序的函数转换为DLL,这样我就不必重做已经完成的工作,因为我想学习并获得编写DLL和使用互操作的经验 到目前为止,每个函数只有一个输入或输出。然而,我的最后两个函数(它获取文件名列表和文件本身的哈希值,并为每个文件获取哈希值,然后将其保存到xml文件)对我来说不是很好。我得到了输出,但不是我想要的形式,所以很明显我的逻辑有缺陷,但我不知道如何纠正它 这些函数非常相似,唯一的区别是前者创建一个全新的xml,而后者更新一个现有的xml。它们以前都是直接的C++/CLI函数,但现在我需要在C#中使用它们,所以我必须将它们放在迭代中,以便它们可以用于列表中显示的每个文件及其哈希值 下面是Dll中的一个函数,我现在将以前的硬编码变量(listBox2->Items[x]->ToString())替换为变量,例如.cs中定义的CurrentFileC# C++/用于WPF表单的Dll中的CLI XML方法,c#,wpf,xml,interop,c++-cli,C#,Wpf,Xml,Interop,C++ Cli,很抱歉发了这么长的帖子,输出量很大。最近我开始测试C#和WPF,我真的很享受它为视觉效果提供的创造力和自由。我开始将当前正在处理的应用程序的函数转换为DLL,这样我就不必重做已经完成的工作,因为我想学习并获得编写DLL和使用互操作的经验 到目前为止,每个函数只有一个输入或输出。然而,我的最后两个函数(它获取文件名列表和文件本身的哈希值,并为每个文件获取哈希值,然后将其保存到xml文件)对我来说不是很好。我得到了输出,但不是我想要的形式,所以很明显我的逻辑有缺陷,但我不知道如何纠正它 这些函数非常
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;
}
}
方法调用:
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());
}
}
private void按钮4\u单击(对象发送者,路由目标)
{
DllTest.Funtions Functions=新的DllTest.Funtions();
字符串名称=textBox1.Text;
字符串TC=textBox2.Text;
String path=“C:\\Users\\brandonm\\Desktop\\Backup\\XML\\test1.XML”;
int Number=listBox2.Items.Count;
对于(int x=0;x
这一个的输出更接近我需要的,因为它使用了列表中的所有项目,但它在每个元素中重复它们。我只想要一个包含新文件及其散列的新元素
<?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 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>
现在你们可以看到问题了,循环中有循环,导致输出重复自身。如果您需要,我可以发布我的输出示例
但对于我来说,我无法绕开它,我曾尝试将函数拆分为多个部分,但我在尝试这样做时出错
有人能想办法解决这个问题吗?我只想完全重写c#中的方法,如果这是我唯一的选择,因为我需要dll中的所有主要函数。请不要因为我缺乏知识而责骂我,我仍在第一次学习和测试很多东西
谢谢解决了问题:)
我将列表中的项目放入一个数组中,然后以这种方式通过它们
下面的代码用于一个函数,但它们都可以使用相同的结构更改
DLL方法创建新xml。DLL中的以下行已更改:
完成:
public: void XMLUpdate(String^ Location, int NumberItems, String^ ProjectName,
String^ ProjectTC, array<String^>^ CurrentFile, array<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[x];
String^ Hash = CurrentHash[x];
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;
}
}
public:void XMLUpdate(字符串^Location,int NumberItems,字符串^ProjectName,
字符串^ProjectTC,数组^CurrentFile,数组^CurrentHash)
{
尝试
{
XmlDocument^XmlDoc=gcnew XmlDocument();
XmlDoc->Load(位置);
xmlement^NewProject=XmlDoc->CreateElement(“项目”);
新建项目->设置属性(“名称”,项目名称);
XmlDoc->DocumentElement->AppendChild(新建项目);
xmlement^NewTestCycle=XmlDoc->CreateElement(“TestCycle”);
NewTestCycle->SetAttribute(“编号”,项目TC);
新建项目->追加子项(新建测试周期);
xmlement^NewFile=XmlDoc->CreateElement(“文件”);
NewTestCycle->AppendChild(新文件);
对于(int x=0;xCreateElement(“文件名”);
NewFileName->SetAttribute(“文件”,文件名);
NewFile->AppendChild(NewFileName);
xmlement^NewHashCode=XmlDoc->CreateElement(“HashCode”);
NewHashCode->SetAttribute(“代码”,哈希);
NewFile->AppendChild(NewHashCode);
}
XmlDoc->保存(位置);
返回;
}
捕获(例外^)
{
返回;
}
}
改变了什么:
public: void XMLNew(String^ Path, String^ ProjectName, String^ TCNumber, int NumberItems, array<String^>^ CurrentFile, array<String^>^ CurrentHashCode)
public:void XMLNew(字符串^Path,字符串^ProjectName,字符串^TCNumber,int NumberItems,数组^CurrentFile,数组^CurrentHashCode)
及
for(int x=0;x
然后调用函数在C#中更改为:
private void按钮4\u单击(对象发送者,路由目标)
{
DllTest.Funtions Functions=新的DllTest.Funtions();
字符串名称=textBox1.Text;
字符串TC=textBox2.Text;
String path=“C:\\Users\\brandonm\\Desktop\\Backup\\XML\\test1.XML”;
int Number=listBox2.Items.Count;
字符串[]文件=新字符串[编号];
字符串[]哈希=新字符串[数字];
对于(int i=0;i <?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 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>
public: void XMLUpdate(String^ Location, int NumberItems, String^ ProjectName,
String^ ProjectTC, array<String^>^ CurrentFile, array<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[x];
String^ Hash = CurrentHash[x];
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;
}
}
public: void XMLNew(String^ Path, String^ ProjectName, String^ TCNumber, int NumberItems, array<String^>^ CurrentFile, array<String^>^ CurrentHashCode)
for (int x = 0; x < NumberItems; ++x)
{
String^ FileName = CurrentFile[x];
String^ Hash = CurrentHashCode[x];
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;
String[] Files = new String[Number];
String[] Hashes = new String[Number];
for (int i = 0; i < listBox2.Items.Count; i++)
{
object s = listBox2.Items[i];
Files[i] = s.ToString();
}
for (int j = 0; j < listBox3.Items.Count; j++)
{
object s = listBox3.Items[j];
Hashes[j] = s.ToString();
}
Functions.XMLNew(path, Name, TC, Number, Files, Hashes);
}