How to read an XML file using DOM API's in a Windows Metro App in C++. RRS feed

All replies

  • Did you look at the Windows::Data::Xml::Dom namespace?
    Monday, July 9, 2012 4:32 PM
  • Hi Yashpal,

    Please have a look WinRT class's available for XML file handling.

    MSDN - http://msdn.microsoft.com/en-us/library/windows/apps/windows.data.xml.dom.aspx

    There are many class's and interfaces available like XmlDocument, etc

    Hope this helps you to handle an XML file from Metro App.



    Monday, July 9, 2012 5:04 PM
  • thanks BhashTech & Sridhar,

    I don't have any problem with the DOM, the main thing is the inclusion of Asynchronous programming in WinRT API's, I find it a bit confusing, also there is no sample available at MSDN using C++.

    I want to create some metro style app that will read and process a local XML file. Any example would be helpful.

    thanks in advance.

    Monday, July 9, 2012 6:09 PM
  • void FileIo::getXML( )
    	using namespace Windows::Storage;
    	using namespace concurrency;
    	typedef Windows::Storage::StorageFile^			 PickSingleFileType;
    	typedef Windows::Data::Xml::Dom::XmlDocument ^   LoadFromFileRetType;
    	auto picker= ref new Windows::Storage::Pickers::FileOpenPicker();
       	picker->FileTypeFilter->Append(".xml");	// Required 
    	task< PickSingleFileType > getFileTask ( picker->PickSingleFileAsync() ); 
    	getFileTask.then([]( PickSingleFileType xmlFile ) 
    		if ( xmlFile != nullptr) 
    			auto doc= ref new Windows::Data::Xml::Dom::XmlDocument();	
    			task< LoadFromFileRetType > getDocTask ( doc->LoadFromFileAsync( xmlFile ) ); 
    			getDocTask.then([ doc ]( LoadFromFileRetType xmlDoc ) 
    				// Now go process the XML file as you like
    				Platform::String ^ nodeName= doc->NodeName;
    My attempt is above. I also did a #include <ppltasks.h>   I am uncertain if this is the best way, but at least it is something to look at.
    Monday, July 9, 2012 8:49 PM
  • Thanks @Andrew.

    It really helped a lot.

    Additionally, can it be designed in a way that I can get the loaded XmlDocument instead of void. I am talking about some function that will have a signature like

    Windows::Data::Xml::Dom::XmlDocument   LoadDocFromFile (Platform::String^ folder, Platform::String^ file);

    (where folder/file is the path to the XML file to be loaded)

    using the concepts of asynchronous programming i.e. using task, task::then.


    Tuesday, July 10, 2012 4:03 AM
  • Hi Yashpal,

    See Andrew's post below explaining how to use PPL Tasks to load the XML file and get access to the DOM. In order to get access to the DOM, have it as a member variable and capture that in the lambda scope. that way once you return from the nested tasks, you are good to go.

    Take a look at http://sridharpoduri.com/2012/04/08/async-programming-using-c-cx/ which shows a very simple example of using tasks. In the example at the site, the "this" pointer is captured in the lambda scope and thus member variables can be referenced inside the lambda.

    Hope this helps.

    Tuesday, July 10, 2012 2:32 PM
  • The following code is my attempt to PASS IN a lambda that is called when the XMLDocument is ready.   I used std::function, and there are other ways too.  I'm still not too familiar with the syntax yet.   I am wary of an attempt to return an XMLDocument because I don't understand how it is possible to delay the return of getXML() until the XML document is ready.  The idea behind the concurrency::task  and the .then is to arrange for future execution that does not hold up this thread after all. 

    Perhaps a PPL expert can comment?

    void FileIo::getXML( std::function< void ( Windows::Data::Xml::Dom::XmlDocument ^ passedDoc ) > myXmlDocHandler ) { using namespace Windows::Storage; using namespace Windows::Storage::Pickers; using namespace Windows::Data::Xml::Dom; using namespace concurrency; auto picker= ref new FileOpenPicker(); picker->FileTypeFilter->Append(".xml"); // Required task< StorageFile^ > getFileTask ( picker->PickSingleFileAsync() ); getFileTask.then([ myXmlDocHandler ]( StorageFile^ xmlFile ) { if ( xmlFile != nullptr) { auto doc= ref new XmlDocument(); task< XmlDocument ^ > getDocTask ( doc->LoadFromFileAsync( xmlFile ) ); getDocTask.then( [ myXmlDocHandler ] ( XmlDocument^ doc ) { myXmlDocHandler( doc ); }); } }); } //--------------------------------------

    // Calling mechanism auto lambda = []( Windows::Data::Xml::Dom::XmlDocument ^ xmlDoc ) { // Now go process the XML file as you like Platform::String ^ nodeName= xmlDoc->NodeName; }; FileIo::getXML( lambda );

    Error handling could perhaps be delegated to another std::function object.

    @Sridhar, the technique of using a member variable that can be captured by the Lambda would also work, but I think Yashpal is thinking in terms of a library routine not dependent on a particular class's member variable.

    • Edited by Andrew7Webb Tuesday, July 10, 2012 9:45 PM code written
    Tuesday, July 10, 2012 2:34 PM
  • @Andrew: ah, I get it.

    @Yashpal: Artur has a great post explaining about PPL tasks at http://msdn.microsoft.com/en-us/magazine/hh781020.aspx. Some of the examples he illustrates talk about returning values from PPL tasks and lambdas.

    Friday, July 13, 2012 6:40 AM
  • @Sridhar

    I tried to navigate to the mentioned link, but it shows following error.


    Yashpal S.

    Server Error in '/' Application.

    The resource cannot be found.

    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

    Requested URL: /en-us/magazine/hh781020.aspx.

    Friday, August 10, 2012 6:46 AM
  • Here are some useful links for folks on this thread:

    1. Perils of lambda capture with PPL tasks: http://blogs.msdn.com/b/nativeconcurrency/archive/2012/01/29/perils-of-lambda-capture.aspx

    2. Asynchronous Programming in C++ Using PPL http://msdn.microsoft.com/en-us/magazine/hh781020.aspx

    3. Here is a video on Windows 8 async programming using C++: http://channel9.msdn.com/Events/Windows-Camp/Developing-Windows-8-Metro-style-apps-in-Cpp/Async-made-simple-with-Cpp-PPL

    4. Here is good blog on the general async philosophy on Windows 8: http://blogs.msdn.com/b/windowsappdev/archive/2012/03/20/keeping-apps-fast-and-fluid-with-asynchrony-in-the-windows-runtime.aspx

    5. Here is the official msdn documentation: http://msdn.microsoft.com/en-us/library/windows/apps/Hh780559.aspx

    Rahul V. Patil

    Friday, August 10, 2012 6:22 PM