Click here to Skip to main content
Licence 
First Posted 31 Oct 2001
Views 57,894
Bookmarked 19 times

Serializing All Data in a Window into XML

By | 31 Oct 2001 | Article
Serializing data into an XML string in a window dialog

The Method

Create an empty Visual C++ 6.0 dialog-based application using the MFC wizard. Add as many different controls as possible to the application (include edit boxes, drop down list controls, check boxes, radio buttons, date control, etc.).

Add a button called IDC_SERIALIZE and double-click on it to add the function below. Compile, run and fill in random data. When you click the serialize button, the application will get all data filled from all the controls and copy them into a CString that gets displayed. This is very useful for those times when you need to capture all data in a window.

Note: This method does not handle all the different control types available.

void CTestDlg::OnSerialize() 
{
    int nCount = 0;
    CString csText, csXMLReport;
    CWnd* pWndInitial = GetFocus();
    CWnd* pWnd = pWndInitial;
    char Name[51];

    csXMLReport = "<?xml version='1.0'?>\n";
    csXMLReport += "<report xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\n";
    ::SendMessage(this->m_hWnd, WM_NEXTDLGCTL, 0, 0);
    pWnd = GetFocus();
    GetClassName(pWnd->m_hWnd, Name, 50);

    while (pWnd != pWndInitial)
    {
        pWnd->GetWindowText(csText);
        csText.TrimLeft();

        if (strcmp(Name,"Button") == 0)
        {
            if (IsDlgButtonChecked(pWnd->GetDlgCtrlID()))
            {
                csXMLReport += "<" + csText + ">";
                csXMLReport += "1";
                csXMLReport += "</" + csText + ">\n";
            }
            else
            {
                CheckDlgButton(pWnd->GetDlgCtrlID(), BST_CHECKED);

                if (IsDlgButtonChecked(pWnd->GetDlgCtrlID()))
                {
                    csXMLReport += "<" + csText + ">";
                    csXMLReport += "0";
                    csXMLReport += "</" + csText + ">\n";
                }
            }
        }
        else if (strcmp(Name,"Edit") == 0)
        {
            if (csText == "")
                csText = "NULL";

            csXMLReport += "<" + csText + "/>\n";
        }
        else if (strcmp(Name,"SysDateTimePick32") == 0)
        {
            csXMLReport += "<Date>" + csText + "</Date>\n";
        }

        ::SendMessage(this->m_hWnd, WM_NEXTDLGCTL, 0, 0);
        pWnd = GetFocus();
        GetClassName(pWnd->m_hWnd, Name, 50);
    }

    csXMLReport += "</report>";
    MessageBox(csXMLReport, MB_OK, NULL);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Bassam Abdul-Baki



United States United States

Member

Follow on Twitter Follow on Twitter
Bassam Abdul-Baki has a Bachelor of Science (BS) degree and a Master of Science (MS) degree in Mathematics and another MS in Technology Management. He's an analyst by trade. He started out in Quality Assurance (QA) and analysis, then dabbled in Visual C++ and Visual C# programming for a while, and then came back to QA and analysis again. He's not sure where he'll be five years from now, but is looking into data analytics.
 
Bassam is into mathematics, astronomy, technology, and genealogy.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSaving XML into a file Pinmembergabriel92712:46 16 Aug '05  
GeneralRe: Saving XML into a file PinmemberBassam Abdul-Baki13:39 16 Aug '05  
GeneralRe: Saving XML into a file Pinmembergabriel9276:17 19 Aug '05  
GeneralRe: Saving XML into a file PinmemberBassam Abdul-Baki17:47 19 Aug '05  
QuestionWhat is the use? Pinmembervadivhere6:12 11 May '05  
AnswerRe: What is the use? Pinmembergabriel9277:42 5 Aug '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 1 Nov 2001
Article Copyright 2001 by Bassam Abdul-Baki
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid