如何将XmlTextreader字符串保存在C+/cli中的变量或向量中?
我目前正在研究一个项目,在该项目中,我必须收到一个XML文件并对其中一个元素进行分类。
在真正到达排序部分之前,我必须解析XML文件,因此我使用XMLTEXTREADER
,它运行良好。但是,我需要将每个元素的属性保存在变量或向量中,以便之后能够执行该排序(我的努力是尝试存储Reader-> value
)。
这是我的代码的一部分,有什么想法吗?
PS您可以在下面的第二个switch
情况下看到我的挣扎,我一直在所有这些尝试中都会收到错误。
#include "pch.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <tchar.h>
#using <System.Windows.Forms.dll>
#using <mscorlib.dll>
using namespace std;
using namespace System;
using namespace System::Xml;
int main() {
string myText = "";
vector<string> entries = { "DeTal" };
entries.insert(entries.end(), "Fulano");//test purposes
XmlTextReader^ reader = gcnew XmlTextReader("C:\\Users...");
while (reader->Read())
{
switch (reader->NodeType)
{
case XmlNodeType::Element: // The node is an element.
Console::Write("<{0}", reader->ReadToFollowing("TITLE"));
while (reader->MoveToNextAttribute()) {// Read the attributes.
Console::Write(" {0}='{1}'", reader->GetAttribute("TITLE"));
}
Console::WriteLine(">");
break;
case XmlNodeType::Text: //Display the text in each element.
Console::WriteLine(reader->Value); //reads the actual element content
//entries.insert(entries.end(), reader->Value);
//entries.push_back(reader->Value);
//myText = Console::WriteLine(reader->Value);
//myText = Console::WriteLine(reader->ReadString());
//myText = reader->Value;
break;
case XmlNodeType::EndElement: //Display the end of the element.
Console::Write("</{0}", reader->Name);
Console::WriteLine(">");
break;
}
}
cout << "\nPress enter to start sort: ";
Console::ReadLine();
I am currently working on a project in which I have to receive a XML file and sort one of the elements.
Before actually getting to the sorting part, I have to parse the XML file, so I am using XmlTextReader
, which is working well. However, I need to save each element's attribute in a variable or in a vector to be able to perform the sort afterwards (my struggle is with trying to store the reader->Value
).
Here is part of my code, any ideas?
P.S. You can see my struggle in the second switch
case below, I kept receiving errors on all those attempts.
#include "pch.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <tchar.h>
#using <System.Windows.Forms.dll>
#using <mscorlib.dll>
using namespace std;
using namespace System;
using namespace System::Xml;
int main() {
string myText = "";
vector<string> entries = { "DeTal" };
entries.insert(entries.end(), "Fulano");//test purposes
XmlTextReader^ reader = gcnew XmlTextReader("C:\\Users...");
while (reader->Read())
{
switch (reader->NodeType)
{
case XmlNodeType::Element: // The node is an element.
Console::Write("<{0}", reader->ReadToFollowing("TITLE"));
while (reader->MoveToNextAttribute()) {// Read the attributes.
Console::Write(" {0}='{1}'", reader->GetAttribute("TITLE"));
}
Console::WriteLine(">");
break;
case XmlNodeType::Text: //Display the text in each element.
Console::WriteLine(reader->Value); //reads the actual element content
//entries.insert(entries.end(), reader->Value);
//entries.push_back(reader->Value);
//myText = Console::WriteLine(reader->Value);
//myText = Console::WriteLine(reader->ReadString());
//myText = reader->Value;
break;
case XmlNodeType::EndElement: //Display the end of the element.
Console::Write("</{0}", reader->Name);
Console::WriteLine(">");
break;
}
}
cout << "\nPress enter to start sort: ";
Console::ReadLine();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
reader-&gt; value
是.netsystem :: String
(16bit Unicode String)。您正在尝试将其分配给,然后将其存储在
std :: vector
的 std :: String (8bit String)。这两种字符串类型并非直接兼容,这就是为什么您遇到麻烦的原因。
您需要要么:
将您的
myText
变量更改为system :: String
,然后您的条目
vector持有system: :string
elements。convert
system :: String
数据到std :: String
。在MSDN上记录了如何做到这一点:如何:将系统::字符串转换为标准字符串。另请参阅如何将System :: String^变成STD :: String?在Stackoverflow上。reader->Value
is a .NETSystem::String
(a 16bit Unicode string).You are trying to assign it to, and store it in a
std::vector
of,std::string
(an 8bit string).Those two string types are not directly compatible with each other, which is why you are having troubles.
You need to either:
change your
myText
variable toSystem::String
, and yourentries
vector to holdSystem::String
elements.convert the
System::String
data tostd::string
. How to do that is documented on MSDN: How to: Convert System::String to Standard String. Also see How to turn System::String^ into std::string? on StackOverflow.