0

我正在尝试遍历 std::list 并将数据从 DLL 中的线程写入 xml。但是,如果我将更多对象字段转储到 XML 中,则不会创建我的 XML。下面是我的课程的详细信息以及我用来将数据写入 XML 的代码。

class MyClass{
private:
    BSTR member1;
    BSTR member2;
    BSTR member3;
    BSTR member4;
public:
    MyClass();
    ~MyClass();
    BSTR getMember1()
    {
        return member1;
    }
    BSTR getMember2()
    {
        return member2;
    }
    BSTR getMember3()
    {
        return member3;
    }
    BSTR getMember4()
    {
        return member4;
    }
    //Corresponding setters are also present
};

std::list<MyClass> myList;
std::list<MyClass>::iterator iterator;

ofstream outputFile;
outputFile.open("C:\\test.xml",ios::out);
outputFile << "<root>" << endl;
for(iterator=myList.begin();iterator != myList.end();++iterator)
{
     //Not working if more values are dumped
     USES_CONVERSION;
     outputFile << "<Object>" << endl;
     outputFile << "<Member1>" << W2A(CString((*iterator).getMember1())) << "</Member1>" << endl;
     outputFile << "<Member2>" << W2A(CString((*iterator).getMember2())) << "</Member2>" << endl;
     outputFile << "<Member3>" << W2A(CString((*iterator).getMember3())) << "</Member3>" << endl;
     outputFile << "<Member4>" << W2A(CString((*iterator).getMember4())) << "</Member4>" << endl;
     outputFile << "</Object>" << endl;
}
outputFile << "</root>";
outpuFile.close();

有人可以帮我吗?

4

1 回答 1

0

我找到了原因,这是因为我正在写入文件而不检查值是否为NULL。对于某些对象,所有成员都未填充。

 outputFile << "<Member3>" << W2A(CString((*iterator).getMember3())) << "</Member3>" << endl;

在写入文件之前,我应该检查getMember3()是否提供任何数据或 NULL :)。

/哈雷什

于 2012-10-25T11:45:25.390 回答