如何将XmlTextreader字符串保存在C+/cli中的变量或向量中?

发布于 2025-02-06 18:19:31 字数 2041 浏览 5 评论 0原文

我目前正在研究一个项目,在该项目中,我必须收到一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹊巢 2025-02-13 18:19:31

reader-&gt; value是.net system :: String(16bit Unicode String)。

您正在尝试将其分配给,然后将其存储在std :: vector的 std :: String (8bit String)。

这两种字符串类型并非直接兼容,这就是为什么您遇到麻烦的原因。

您需要要么:

reader->Value is a .NET System::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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文