VC read/write XML

Source: Internet
Author: User

Today, we have performed some simple read/write operations on the XML file. The specific implementation steps are as follows:

First, create a dialog-based MFC project named operatexml, and then add two buttons ("CREATE" and "getdata" and a listcontrol
Control (used to display data), first import to the database file
# Import "msxml3.dll"
Using namespace msxml2;
It is mainly used to perform operations on XML files,
Double-click the "Create" button to write the code as follows:
Void coperatexml: onbtncreate ()
{
// Todo: add your control notification handler code here
: Coinitialize (null); // initialize com
Msxml2: ixmldomdocumentptr pdoc;
Msxml2: ixmldomelementptr xmlroot;
Hresult hR = pdoc. createinstance (_ uuidof (msxml2: domdocument30 ));
If (! Succeeded (HR ))
{
MessageBox ("error ");
Return;
}
Pdoc-> raw_createelement (_ bstr_t) (char *) "China", & xmlroot );
Pdoc-> raw_appendchild (xmlroot, null );

Msxml2: ixmldomelementptr childnode;
Pdoc-> raw_createelement (_ bstr_t) (char *) "city", & childnode );
Childnode-> puttext ("Shanghai ");
Childnode-> setattribute ("population", "7000 ");
Childnode-> setattribute ("area", "2000 ");
Xmlroot-> appendchild (childnode );

Pdoc-> raw_createelement (_ bstr_t) (char *) "city", & childnode );
Childnode-> puttext ("Beijing ");
Childnode-> setattribute ("population", "39999 ");
Childnode-> setattribute ("area", "3322 ");
Xmlroot-> appendchild (childnode );

Pdoc-> Save ("D: // test. xml ");
}

Double-click the "getdata" button to write the code as follows:
Void coperatexml: onbtnget ()
{
// Todo: add your control notification handler code here
M_list.deleteallitems ();
Msxml2: ixmldomdocumentptr pdoc;
Hresult hR = pdoc. createinstance (_ uuidof (msxml2: domdocument30 ));
If (! Succeeded (HR ))
{
MessageBox ("error! ");
Return;
}
Pdoc-> load ("D: // test. xml ");
Msxml2: ixmldomelementptr childnode;
Childnode = (msxml2: ixmldomelementptr) (pdoc-> selectsinglenode ("// City "));

Msxml2: domnodetype nodetype;
Childnode-> get_nodetype (& nodetype );

Msxml2: ixmldomnamednodemapptr pattrs = NULL;
Msxml2: ixmldomnodelistptr nodelist;
Msxml2: ixmldomnodeptr pattritem;
Childnode-> get_attributes (& pattrs );
Childnode-> get_childnodes (& nodelist );
Long ncount, icount;
Pattrs-> get_length (& ncount); // gets the number of node attributes
Nodelist-> get_length (& icount); // obtain the number of nodes
For (INT I = 0; I <icount; I ++) // you can add data to listcontrol as needed.
{
For (Int J = 0; j <ncount; j ++)
{
Pattrs-> get_item (J, & pattritem );
Cstring strattrname = (char *) (_ bstr_t) pattritem-> nodename;
Cstring strattrvalue = (char *) (_ bstr_t) pattritem-> nodetypedvalue;
M_list.insertitem (I, strattrname );
M_list.setitemtext (I, 1, strattrvalue );
}
}

}

Binds an operation variable to the listcontrol control.

Clistctrl m_list;

Initialize oninitdialog with the following code:

Bool coperatexml: oninitdialog ()
{
// Todo: add extra initialization here
M_list.insertcolumn (0, "Population ");
M_list.insertcolumn (1, "num ");
M_list.insertcolumn (1, "area ");
M_list.insertcolumn (1, "num ");
Crect rect;
M_list.getclientrect (& rect );
Int width = (rect. Right-rect. Left)/4;
M_list.setcolumnwidth (0, width );
M_list.setcolumnwidth (1, width );
M_list.setcolumnwidth (2, width );
M_list.setcolumnwidth (3, width );
}

F7 Compilation Program, CTRL + F5 to view the display results, so far a simple XML file operation has been completed successfully.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.