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

.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::Schema;
  9. __gc class ValHandler
  10. {
  11. public:
  12. static void ValidationHandler(Object* pSender, ValidationEventArgs* pe)
  13.     {
  14.         Console::WriteLine("Validation Event: {0}", pe->Message);
  15.     }
  16. };
  17. // This is the entry point for this application
  18. #ifdef _UNICODE
  19. int wmain(void)
  20. #else
  21. int main(int argc, char* argv[])
  22. #endif
  23. {
  24.     // Check for required arguments
  25.     if (argc < 2)
  26.     {
  27.         Console::WriteLine(S"Usage: CppTextReader path");
  28.         return -1;
  29.     }
  30.     String* path = new String(argv[1]);
  31.     try
  32.     {
  33.         // Create the reader...
  34.         XmlTextReader* rdr = new XmlTextReader(path);
  35.         // Read nodes
  36.         while (rdr->Read())
  37.         {
  38.             switch (rdr->NodeType)
  39.             {
  40.             case XmlNodeType::XmlDeclaration:
  41.                 Console::WriteLine("-> XML declaration");
  42.                 break;
  43.             case XmlNodeType::Document:
  44.                 Console::WriteLine("-> Document node");
  45.                 break;
  46.             case XmlNodeType::Element:
  47.                 Console::WriteLine("-> Element node, name={0}", rdr->Name);
  48.                 if (rdr->AttributeCount > 0)
  49.                 {
  50.                     Console::Write("  ");
  51.                     while (rdr->MoveToNextAttribute())
  52.                         Console::Write(" {0}={1}", rdr->Name, rdr->Value);
  53.                     Console::WriteLine();
  54.                 }
  55.                 break;
  56.             case XmlNodeType::EndElement:
  57.                 Console::WriteLine("-> End element node, name={0}", rdr->Name);
  58.                 break;
  59.             case XmlNodeType::Text:
  60.                 Console::WriteLine("-> Text node, value={0}", rdr->Value);
  61.                 break;
  62.             case XmlNodeType::Comment:
  63.                 Console::WriteLine("-> Comment node, name={0}, value={1}",
  64.                                    rdr->Name, rdr->Value);
  65.                 break;
  66.             case XmlNodeType::Whitespace:
  67.                 break;
  68.             default:
  69.                 Console::WriteLine("** Unknown node type");
  70.                 break;
  71.             }
  72.         }
  73.         // Create a new reader
  74.         XmlTextReader* rdr2 = new XmlTextReader(path);
  75.         
  76.         // Create the validating reader...
  77.         Console::WriteLine();
  78.         Console::WriteLine("Validated XML:");
  79.         
  80.         // Create the validating reader and set the validation type
  81.         XmlValidatingReader* xvr = new XmlValidatingReader(rdr2);
  82.         xvr->ValidationType = ValidationType::Auto;
  83.         // Set the handler
  84.         xvr->ValidationEventHandler += 
  85.             new ValidationEventHandler(0, &ValHandler::ValidationHandler);
  86.         // Read nodes from the XmlValidatingReader
  87.         while (xvr->Read())
  88.         {
  89.             switch (xvr->NodeType)
  90.             {
  91.             case XmlNodeType::XmlDeclaration:
  92.                 Console::WriteLine("-> XML declaration");
  93.                 break;
  94.             case XmlNodeType::Document:
  95.                 Console::WriteLine("-> Document node");
  96.                 break;
  97.             case XmlNodeType::Element:
  98.                 Console::WriteLine("-> Element node, name={0}", xvr->Name);
  99.                 break;
  100.             case XmlNodeType::EndElement:
  101.                 Console::WriteLine("-> End element node, name={0}",
  102.                                 xvr->Name);
  103.                 break;
  104.             case XmlNodeType::Text:
  105.                 Console::WriteLine("-> Text node, value={0}", xvr->Value);
  106.                 break;
  107.             case XmlNodeType::Comment:
  108.                 Console::WriteLine("-> Comment node, name={0}, value={1}",
  109.                                 xvr->Name, xvr->Value);
  110.                 break;
  111.             case XmlNodeType::Whitespace:
  112.                 break;
  113.             default:
  114.                 Console::WriteLine("** Unknown node type");
  115.                 break;
  116.             }
  117.         }
  118.     }
  119.     catch (Exception* pe)
  120.     {
  121.         Console::WriteLine(pe->ToString());
  122.     }
  123.     return 0;
  124. }