CppTextReader.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:4k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- #using <System.xml.dll>
- using namespace System;
- using namespace System::Xml;
- using namespace System::Xml::Schema;
- __gc class ValHandler
- {
- public:
- static void ValidationHandler(Object* pSender, ValidationEventArgs* pe)
- {
- Console::WriteLine("Validation Event: {0}", pe->Message);
- }
- };
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(int argc, char* argv[])
- #endif
- {
- // Check for required arguments
- if (argc < 2)
- {
- Console::WriteLine(S"Usage: CppTextReader path");
- return -1;
- }
- String* path = new String(argv[1]);
- try
- {
- // Create the reader...
- XmlTextReader* rdr = new XmlTextReader(path);
- // Read nodes
- while (rdr->Read())
- {
- switch (rdr->NodeType)
- {
- case XmlNodeType::XmlDeclaration:
- Console::WriteLine("-> XML declaration");
- break;
- case XmlNodeType::Document:
- Console::WriteLine("-> Document node");
- break;
- case XmlNodeType::Element:
- Console::WriteLine("-> Element node, name={0}", rdr->Name);
- if (rdr->AttributeCount > 0)
- {
- Console::Write(" ");
- while (rdr->MoveToNextAttribute())
- Console::Write(" {0}={1}", rdr->Name, rdr->Value);
- Console::WriteLine();
- }
- break;
- case XmlNodeType::EndElement:
- Console::WriteLine("-> End element node, name={0}", rdr->Name);
- break;
- case XmlNodeType::Text:
- Console::WriteLine("-> Text node, value={0}", rdr->Value);
- break;
- case XmlNodeType::Comment:
- Console::WriteLine("-> Comment node, name={0}, value={1}",
- rdr->Name, rdr->Value);
- break;
- case XmlNodeType::Whitespace:
- break;
- default:
- Console::WriteLine("** Unknown node type");
- break;
- }
- }
- // Create a new reader
- XmlTextReader* rdr2 = new XmlTextReader(path);
-
- // Create the validating reader...
- Console::WriteLine();
- Console::WriteLine("Validated XML:");
-
- // Create the validating reader and set the validation type
- XmlValidatingReader* xvr = new XmlValidatingReader(rdr2);
- xvr->ValidationType = ValidationType::Auto;
- // Set the handler
- xvr->ValidationEventHandler +=
- new ValidationEventHandler(0, &ValHandler::ValidationHandler);
- // Read nodes from the XmlValidatingReader
- while (xvr->Read())
- {
- switch (xvr->NodeType)
- {
- case XmlNodeType::XmlDeclaration:
- Console::WriteLine("-> XML declaration");
- break;
- case XmlNodeType::Document:
- Console::WriteLine("-> Document node");
- break;
- case XmlNodeType::Element:
- Console::WriteLine("-> Element node, name={0}", xvr->Name);
- break;
- case XmlNodeType::EndElement:
- Console::WriteLine("-> End element node, name={0}",
- xvr->Name);
- break;
- case XmlNodeType::Text:
- Console::WriteLine("-> Text node, value={0}", xvr->Value);
- break;
- case XmlNodeType::Comment:
- Console::WriteLine("-> Comment node, name={0}, value={1}",
- xvr->Name, xvr->Value);
- break;
- case XmlNodeType::Whitespace:
- break;
- default:
- Console::WriteLine("** Unknown node type");
- break;
- }
- }
- }
- catch (Exception* pe)
- {
- Console::WriteLine(pe->ToString());
- }
- return 0;
- }