Process XML in Managed C ++

Source: Internet
Author: User
Tags net xml

In this article, I will use the. NET Framework to process XML.

The advantages of using the. net xml function include:

● No installation, configuration, or re-allocation is required. Your apps also need this framework, and everything is ready as long as you have this framework;

● The encoding is simpler, because you are not processing COM. For example, you do not need to call CoInitialize () and CoUninitialize ();

● More features than msxml4.

XML instance

I will illustrate it using the following example:

      
       <?xml version="1.0" encoding="utf-8" ?> <PurchaseOrder><Customer id="123"/><Item SKU="1234" Price="4.56" Quantity="1"/><Item SKU="1235" Price="4.58" Quantity="2"/></PurchaseOrder>
      


Load XML with XmlDocument

Put the classes for processing XML in System: Xml namespace. XmlDocument indicates a DOM file, that is, the subdirectory loaded by XML. This is the same as DOMDocument in the MSXML4 method. The following is a simple Managed C ++ application. XML files are installed in the memory:

      
       #include "stdafx.h"#using <mscorlib.dll>#include <tchar.h>using namespace System;#using <System.Xml.dll>using namespace System::Xml;// This is the entry point for this applicationint _tmain(void){  XmlDocument* xmlDoc = new XmlDocument();  try  {    xmlDoc->Load("sample.xml");    System::Console::WriteLine("Document loaded ok." );  }  catch (Exception *e)  {    System::Console::WriteLine("load problem");    System::Console::WriteLine(e->Message);  }  return 0;}
      


# Using statements are very important. Without it, some strange compilation errors will occur, such as Xml: is not a member of System or Xml: a namespace with this name does not exist. in C # Or VB. NET, there is a Project, Add References menu Project automatically complete this job for you, but in C ++, programmers must complete it by themselves. You can find the class or namespace assembly in online help.

Note that the Load () return value is not used in encoding as in the COM method. If XML cannot be loaded, the loader reports an exception.

Simple Algorithm of Document Content

Here is the corresponding encoding in the. NET method.

      
       xmlDoc->Load("sample.xml");double total = 0;System::Console::WriteLine("Document loaded ok." );XmlNodeList* items = xmlDoc->GetElementsByTagName("Item");long numitems = items->Count;for (int i=0;i<numitems;i++){  XmlNode* item = items->Item(i);  double price =    Double::Parse(item->Attributes->GetNamedItem("Price")->                  get_Value());  double qty =    Double::Parse(item->Attributes->GetNamedItem("Quantity")->                  get_Value());  total += price * qty;}System::Console::WriteLine("Purchase Order total is ${0}",                           __box(total));
      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.