CppNavigator.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:3k
源码类别:

.net编程

开发平台:

Visual C++

  1. // This is the main project file for VC++ application project 
  2. // generated using an Application Wizard.
  3. #include "stdafx.h"
  4. #using <mscorlib.dll>
  5. #using <System.xml.dll>
  6. using namespace System;
  7. using namespace System::Xml;
  8. using namespace System::Xml::XPath;
  9. // This is the entry point for this application
  10. #ifdef _UNICODE
  11. int wmain(void)
  12. #else
  13. int main(int argc, char* argv[])
  14. #endif
  15. {
  16.     // Check for required arguments
  17.     if (argc < 2)
  18.     {
  19.         Console::WriteLine(S"Usage: CppNavigator path");
  20.         return -1;
  21.     }
  22.     String* path = new String(argv[1]);
  23.     try
  24.     {
  25.         // Create the XmlDocument to parse the file
  26.         XmlDocument* doc = new XmlDocument();
  27.         // Load the file
  28.         doc->Load(path);
  29.         Console::WriteLine("Document loaded");
  30.         // Create the navigator
  31.         XPathNavigator* nav = doc->CreateNavigator();
  32.         // Move to the top of the tree and print details
  33.         nav->MoveToRoot();
  34.         Console::WriteLine("top: name={0}, type={1}, value={2}", nav->Name, 
  35.                             __box(nav->NodeType)->ToString(),
  36.                             nav->Value);
  37.         // Move to the first child, which is a comment
  38.         nav->MoveToFirstChild();
  39.         Console::WriteLine("first child: name={0}, type={1}", nav->Name, 
  40.                                         __box(nav->NodeType)->ToString());
  41.         // Move to the next element, which is the root element
  42.         nav->MoveToNext();
  43.         Console::WriteLine("next child: name={0}, type={1}", nav->Name, 
  44.                                         __box(nav->NodeType)->ToString());
  45.         // Move to the next element, which will be the first volcano
  46.         nav->MoveToFirstChild();
  47.         Console::WriteLine("next child: name={0}, type={1}", nav->Name, 
  48.                                         __box(nav->NodeType)->ToString());
  49.         if (nav->HasAttributes)
  50.         {
  51.             nav->MoveToFirstAttribute();
  52.             Console::WriteLine("  attribute: name={0}, type={1}", 
  53.                                 nav->Name, nav->Value);
  54.             nav->MoveToParent();
  55.         }
  56.         // Move back to the root
  57.         Console::WriteLine(S"XPath test...");
  58.         nav->MoveToRoot();
  59.         XPathNodeIterator* xpi = nav->Select("/geology/volcano[comment]");
  60.         Console::WriteLine(S"Select returned {0} nodes", __box(xpi->Count));
  61.         while (xpi->MoveNext())
  62.         {
  63.             XPathNavigator* xpn = xpi->Current;
  64.             Console::Write("node: name={0}, type={1}", xpn->Name, 
  65.                         __box(xpn->NodeType)->ToString());
  66.             xpn->MoveToFirstAttribute();
  67.             Console::WriteLine(", name={0}", xpn->Value);
  68.         }
  69.     }
  70.     catch(Exception* pe)
  71.     {
  72.         Console::WriteLine(pe->Message);
  73.         return 0;
  74.     }
  75.     return 0;
  76. }