locked
How do I parse XML containing custom namespaces?

    Question

  • Hi,

    I need to parse an existing XML in windows store app using c++, which contains some custom namespaces.

    like : <dd:Revision>AMDSKKDSFKSDF</dd:Revision>. How can I do that?

    Thanks 

    Thursday, September 27, 2012 10:16 AM

Answers

  • Not sure if you have the same situation as in this sample file.  Here is a sample xml file( trial.xml ) with namespace:

    <?xml version='1.0'?>
    <Books xmlns="urn:Books" xmlns:pub="urn:Publisher">
    <Book>
      <pub:Publisher>Wrox</pub:Publisher>
    </Book>
    </Books>

    Hope the following helps:

    Windows::Storage::StorageFolder^ f = KnownFolders::PicturesLibrary;
    	task<StorageFile^>(f->GetFileAsync( "Trial.xml" )).then([this](task<StorageFile^> t)
    	{
    		StorageFile^ f1 = dynamic_cast<StorageFile^>(t.get());
    		IAsyncOperation<XmlDocument^>^LoadXML = XmlDocument::LoadFromFileAsync( f1 );
    		task<XmlDocument^>op( LoadXML );
    		op.then([this]( XmlDocument^ doc )
    		{
    			XmlNodeList^ List = doc->SelectNodesNS( "//pub:Publisher", "xmlns:pub='urn:Publisher'");
    			int nIndex = List->Size;
    			for( int i = 0; i < nIndex ;++i )
    			{
    				IXmlNode^ node = List->GetAt( i );
    				String^ publisher= dynamic_cast<String^>( node->FirstChild->NodeValue );
    			}
    		});
    
    	});

      

    • Marked as answer by Ant_Developer Friday, September 28, 2012 5:42 AM
    Thursday, September 27, 2012 7:37 PM

All replies

  • Not sure if you have the same situation as in this sample file.  Here is a sample xml file( trial.xml ) with namespace:

    <?xml version='1.0'?>
    <Books xmlns="urn:Books" xmlns:pub="urn:Publisher">
    <Book>
      <pub:Publisher>Wrox</pub:Publisher>
    </Book>
    </Books>

    Hope the following helps:

    Windows::Storage::StorageFolder^ f = KnownFolders::PicturesLibrary;
    	task<StorageFile^>(f->GetFileAsync( "Trial.xml" )).then([this](task<StorageFile^> t)
    	{
    		StorageFile^ f1 = dynamic_cast<StorageFile^>(t.get());
    		IAsyncOperation<XmlDocument^>^LoadXML = XmlDocument::LoadFromFileAsync( f1 );
    		task<XmlDocument^>op( LoadXML );
    		op.then([this]( XmlDocument^ doc )
    		{
    			XmlNodeList^ List = doc->SelectNodesNS( "//pub:Publisher", "xmlns:pub='urn:Publisher'");
    			int nIndex = List->Size;
    			for( int i = 0; i < nIndex ;++i )
    			{
    				IXmlNode^ node = List->GetAt( i );
    				String^ publisher= dynamic_cast<String^>( node->FirstChild->NodeValue );
    			}
    		});
    
    	});

      

    • Marked as answer by Ant_Developer Friday, September 28, 2012 5:42 AM
    Thursday, September 27, 2012 7:37 PM
  • Thanks for your reply. It helps a lot.
    Friday, September 28, 2012 5:43 AM