1 int writeXML(CString filename)
2 {
3 CString sm;
4 sm = _T("写XML成功!");
5 HRESULT hr = S_OK;
6 CComVariant FileName;//COM版的Variant;
7
8 CoInitialize(NULL);//必须要初始化COM;
9 IXMLDOMDocumentPtr pXMLDom = NULL;//DOM操作的智能指针;
10 hr = pXMLDom.CreateInstance(__uuidof(DOMDocument60), NULL, CLSCTX_INPROC_SERVER);//DOM对象创建;
11 pXMLDom->put_preserveWhiteSpace(VARIANT_TRUE);
12
13 VARIANT_BOOL VBool;
14 pXMLDom->get_async(&VBool);
15 pXMLDom->get_validateOnParse(&VBool);
16 pXMLDom->get_resolveExternals(&VBool);
17
18 IXMLDOMProcessingInstructionPtr pPI = NULL;
19 pXMLDom->createProcessingInstruction(_T("xml"),_T("version='1.0' encoding='UTF-8'"),&pPI);//声明;
20 pXMLDom->appendChild(pPI,NULL);
21
22 CString root = _T("root");//根节点名字;
23 IXMLDOMElementPtr pRoot = NULL;
24 pXMLDom->createElement(root.GetBuffer(),&pRoot);//根元素;
25 CComVariant var = _T("15");
26 pRoot->setAttribute(_T("IyyyyD"),var);//设置属性;
27
28 { IXMLDOMElementPtr pElement = NULL;
29 pXMLDom->createElement(_T("name"),&pElement);//创建元素;
30 pElement->put_text(_T("li"));//设置文字;
31 CComVariant var2 = _T("13");
32 pElement->setAttribute(_T("ID"),var2 );//设置属性;
33 pRoot->appendChild(pElement,NULL);
34 pXMLDom->appendChild(pRoot,NULL);}//添加节点;
35
36 CreateDirectory(_T(".\\Folder"),NULL);//创建文件夹;
37 CString filename = _T(".\\Folder\\4.xml");//XML文件名;
38 hr = pXMLDom->save(filename);//保存XML文件;
39 CoUninitialize();//COM组件用完;
40 return 0;
41 }