- // 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::Collections;
- __gc class XmlBuilder
- {
- private:
- XmlNode* root;
- XmlDocument* doc;
- XmlNodeList* xnl;
- public:
- XmlBuilder(String* path)
- {
- // Create the XmlDocument
- doc = new XmlDocument();
- // Load the data
- doc->Load(path);
- Console::WriteLine("Document loaded");
- // Get the root of the tree
- root = doc->DocumentElement;
- // get the child node list
- xnl = doc->ChildNodes;
- IEnumerator* ie = xnl->GetEnumerator();
- while (ie->MoveNext() == true)
- Console::WriteLine("Child: {0}",
- (dynamic_cast<XmlNode*>(ie->Current))->Name);
- }
- void processChildNodes()
- {
- // Declare an enumerator
- IEnumerator* ie = xnl->GetEnumerator();
- while (ie->MoveNext() == true)
- {
- // Get a pointer to the node
- XmlNode* pNode = dynamic_cast<XmlNode*>(ie->Current);
- // See if it is the root
- if (pNode->NodeType == XmlNodeType::Element &&
- pNode->Name->Equals(S"geology"))
- {
- Console::WriteLine(" Found the root");
- processRoot(pNode);
- }
- }
- }
- void processRoot(XmlNode* rootNode)
- {
- XmlNode* pVolc = dynamic_cast<XmlNode*>(rootNode->ChildNodes->Item(1));
- // Create a new volcano element
- XmlElement* newVolcano = createNewVolcano();
- // Link it in
- root->InsertBefore(newVolcano, pVolc);
- }
- XmlElement* createNewVolcano()
- {
- // Create a new element
- XmlElement* newElement = doc->CreateElement("volcano");
- // Set the name attribute
- XmlAttribute* pAtt = doc->CreateAttribute("name");
- pAtt->Value = S"Mount St.Helens";
- newElement->Attributes->Append(pAtt);
- // Create the location element
- XmlElement* locElement = doc->CreateElement("location");
- XmlText* xt = doc->CreateTextNode(S"Washington State, USA");
- locElement->AppendChild(xt);
- newElement->AppendChild(locElement);
- return newElement;
- }
- void printTree()
- {
- XmlTextWriter* xtw = new XmlTextWriter(Console::Out);
- xtw->Formatting = Formatting::Indented;
- doc->WriteTo(xtw);
- xtw->Flush();
- Console::WriteLine();
- }
- };
- // 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: CppWriter path");
- return -1;
- }
- String* path = new String(argv[1]);
- try
- {
- XmlBuilder* pf = new XmlBuilder(path);
- pf->processChildNodes();
- pf->printTree();
- }
- catch(Exception* pe)
- {
- Console::WriteLine(pe->Message);
- }
- return 0;
- }