CppNavigator.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:3k
- // 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::XPath;
- // 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: CppNavigator path");
- return -1;
- }
- String* path = new String(argv[1]);
- try
- {
- // Create the XmlDocument to parse the file
- XmlDocument* doc = new XmlDocument();
- // Load the file
- doc->Load(path);
- Console::WriteLine("Document loaded");
- // Create the navigator
- XPathNavigator* nav = doc->CreateNavigator();
- // Move to the top of the tree and print details
- nav->MoveToRoot();
- Console::WriteLine("top: name={0}, type={1}, value={2}", nav->Name,
- __box(nav->NodeType)->ToString(),
- nav->Value);
- // Move to the first child, which is a comment
- nav->MoveToFirstChild();
- Console::WriteLine("first child: name={0}, type={1}", nav->Name,
- __box(nav->NodeType)->ToString());
- // Move to the next element, which is the root element
- nav->MoveToNext();
- Console::WriteLine("next child: name={0}, type={1}", nav->Name,
- __box(nav->NodeType)->ToString());
- // Move to the next element, which will be the first volcano
- nav->MoveToFirstChild();
- Console::WriteLine("next child: name={0}, type={1}", nav->Name,
- __box(nav->NodeType)->ToString());
- if (nav->HasAttributes)
- {
- nav->MoveToFirstAttribute();
- Console::WriteLine(" attribute: name={0}, type={1}",
- nav->Name, nav->Value);
- nav->MoveToParent();
- }
- // Move back to the root
- Console::WriteLine(S"XPath test...");
- nav->MoveToRoot();
- XPathNodeIterator* xpi = nav->Select("/geology/volcano[comment]");
- Console::WriteLine(S"Select returned {0} nodes", __box(xpi->Count));
- while (xpi->MoveNext())
- {
- XPathNavigator* xpn = xpi->Current;
- Console::Write("node: name={0}, type={1}", xpn->Name,
- __box(xpn->NodeType)->ToString());
- xpn->MoveToFirstAttribute();
- Console::WriteLine(", name={0}", xpn->Value);
- }
- }
- catch(Exception* pe)
- {
- Console::WriteLine(pe->Message);
- return 0;
- }
- return 0;
- }