从DLL将数据写入XML

时间:2012-10-25 11:11:07

标签: xml visual-c++ dll file-handling

我正在尝试迭代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();

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

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

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

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

/ Hareesh