Serializing All Data in a Window into XML

Environment: Visual C++ 6.0 SP4 Windows 2000

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);
}

Downloads

None


Comments

  • Restoring data

    Posted by Legacy on 11/09/2001 12:00am

    Originally posted by: alex

    Is is a half of the work,wat are we saving for?

    Reply
  • Maintaining state

    Posted by Legacy on 11/04/2001 12:00am

    Originally posted by: rachan

    Nice way to maintain state of windows in an app

    Reply
  • good practice

    Posted by Legacy on 11/01/2001 12:00am

    Originally posted by: James Lee

    This is good start for you to practice. Also the effort is pretty encouraging.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds