+ Reply to Thread
Results 1 to 2 of 2

Thread: How to read/write XML file in MFC/WIN32 application?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    48

    How to read/write XML file in MFC/WIN32 application?

    I am newbie to XML world and need some help from this forum.

    Can anyone get me some idea how to read/write XML file in MFC/WIN32 application?

    Any help would be much appreciated!

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    24
    Following is the code that can read the below file:

    <?xml version="1.0" encoding="UTF-8"?>
    <Students>
    <Student>
    <RoleID>1</RoleID>
    </Student>
    </Students>


    Code:
    void ReadXMLFile()
    {
    	HRESULT hr = CoInitialize(NULL) ;
        if (SUCCEEDED(hr))
        {
    		try
            {
    			XML::IXMLDOMDocument2Ptr pXmlDoc = NULL ;
                hr = pXmlDoc.CreateInstance(__uuidof(XML::DOMDocument60), NULL, CLSCTX_INPROC_SERVER) ;
                if (SUCCEEDED(hr) && pXmlDoc->load(_T("c:\\MyData.xml")) != VARIANT_TRUE)
    			{
    			}
    			else
                {
    				XML::IXMLDOMNodePtr pStudent = pXmlDoc->selectSingleNode("/Students/Student/*") ;
    
    				if (NULL != pStudent)
    				{
    					XML::IXMLDOMNodePtr pChildNode = pStudent->GetfirstChild() ;
    
    					if (NULL != pChildNode)
    					{
    						BSTR bstrRoleID = pChildNode->Gettext() ;
    					}
    				}
                }
    		}
    		catch(...)
    		{
    
    		}
    	}
    
    	CoUninitialize() ;
    }
    Hope this helps.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts