Read and write XML using MSXML.DLL; add #import at the top of the file"Msxml3.dll";using namespaceMSXML2;//The function of these two sentences is to generate MSXML3.TLH and Msxml3.tli two files under the program folder;---------------------------------------------------------create an XML file;:: CoInitialize (NULL); Msxml2::ixmldomdocumentptr xmldoc; Msxml2::ixmldomelementptr XmlRoot; HRESULT HR=xmldoc. CreateInstance (_uuidof (MSXML2::D OMDocument30)); if(!succeeded (HR)) {MessageBox ("faild!!"); return; } XmlRoot= Xmldoc->createelement ("ROOT"); XmlRoot->setattribute ("ID","12345");//sets the properties of the root tag;Xmldoc->appendchild (XmlRoot); CString TMP; Msxml2::ixmldomelementptr XMLNODE; for(intI=0;i<Ten; i++) {TMP. Format ("%d", I); XMLNODE= Xmldoc->createelement ((_bstr_t) ("NODE"+TMP)); XMLNODE->put_text ((_bstr_t)"nodetexts");//set the text content of the label;Xmlnode->setattribute ("ID", (_variant_t) TMP);//set the properties and contents of the label;Xmlnode->setattribute ("NAME","NODENAME"); XmlRoot-appendchild (XMLNODE);} XmlDoc->save ("XMLTEST. XML"); XMLNODE. Release (); XmlRoot. Release (); XmlDoc. Release (); :: CoUninitialize ();------------------------------------------------------------------reads an XML;:: CoInitialize (NULL); Msxml2::ixmldomdocumentptr xmldoc; Msxml2::ixmldomelementptr XmlRoot; Msxml2::ixmldomelementptr XMLELEMENT; Msxml2::ixmldomnodelistptr XMLNODES; //so the byte point of a nodeMsxml2::ixmldomnamednodemapptr Xmlnodeatts;//all properties of a node;msxml2::ixmldomnodeptr XMLNODE; HRESULT HR=xmldoc. CreateInstance (_uuidof (MSXML2::D OMDocument30)); if(!succeeded (HR)) {MessageBox ("faild!!"); return; } XmlDoc->load ("XMLTEST. XML"); XmlRoot= Xmldoc->getdocumentelement ();//get the root node;Xmlroot->get_childnodes (&xmlnodes);//get all the child nodes of the root node; LongXmlnodesnum,attsnum; XMLNODES->get_length (&xmlnodesnum);//get the number of all child nodes;CString TMP; Tmp. Format ("%d", Xmlnodesnum); MessageBox (TMP); for(intI=0; i<xmlnodesnum;i++) {XMLNODES->get_item (I,&xmlnode);//obtain a child node;Xmlnode->get_attributes (&xmlnodeatts);//get all the properties of a node;Xmlnodeatts->get_length (&attsnum);//get the number of all attributes; for(intj=0; j<attsnum; J + +) {Xmlnodeatts->get_item (J,&xmlnode);//get a property;CString T1 = (Char*) (_bstr_t) xmlnode->NodeName; CString T2= (Char*) (_bstr_t) xmlnode->text; MessageBox (T1+" = "+T2); } //MessageBox ((_bstr_t) xmlnode->nodename+ ":" + (_bstr_t) xmlnode->text); } return; //xmldoc->save ("XMLTEST. XML ");XMLNODES. Release (); XMLNODE. Release (); XmlRoot. Release (); XmlDoc. Release (); :: CoUninitialize ();---------------------------------------------------------Summary: XML file type: msxml2::ixmldomdocumentptr; single element type: Msxml2::ixmldomelementptr; single node type: msxml2::ixmldomnodeptr; All child node types: msxml2::ixmldomnodelistptr; all attribute types: msxml2::ixmldomnamednodemapptr; methods for obtaining the number of nodes: Get_length (); Methods for obtaining all child nodes: get _childnodes (); method for obtaining the specified node: get_item (index number, single node type variable); text content method written to a single node: Put_text ((_bstr_t)"content"); Gets the text content property of a single node: XMLNODE-Gets the content property of a single property: Xmlnodeatt-the property of the node name is: XMLNODE-NodeName;---------------------------------------------------------------XML File Creation General procedure::: CoInitialize (NULL); HRESULT HR=xmldoc. CreateInstance (_uuidof (MSXML2::D OMDocument30)); if(!succeeded (HR)) {MessageBox ("faild!!"); return; } XmlRoot= Xmldoc->createelement ("ROOT");//create a file;Action file .............. XmlDoc->save ("XMLTEST. XML");//Save the file;XMLNODE. Release (); XmlRoot. Release (); XmlDoc. Release (); :: CoUninitialize ();------------------------------------------------------------------------------load file over program::: CoInitialize (NULL); HRESULT HR=xmldoc. CreateInstance (_uuidof (MSXML2::D OMDocument30)); if(!succeeded (HR)) {MessageBox ("faild!!"); return; } XmlDoc->load ("XMLTEST. XML"); file operations ................ XmlDoc->save ("XMLTEST. XML");//can save the changes to the file;XMLNODES. Release (); XMLNODE. Release (); XmlRoot. Release (); XmlDoc. Release (); :: CoUninitialize ();
C + + operation XML file using MSXML.DLL