XML technology to store data and documents is a very easy thing, the. NET Framework in its namespace System.Xml provides a convenient way to manipulate XML class XmlDocument, which is easy to use, XmlDocument is actually a simple tree, let's take a look at about XmlDocument get the first node and delete the root node method
#include "stdafx.h"
using namespace System;
using namespace System::xml;
void Navigate (XmlNode ^node, int depth)
{
if (node = = nullptr)
Return
Console::WriteLine (depth);
Console::WriteLine (Node->nodetype.tostring ());
Console::WriteLine (Node->name);
Console::WriteLine (Node->value);
if (node->attributes!= nullptr)
{
for (int i = 0; i < node->attributes->count; i++)
{ br> Console::WriteLine (depth+1);
Console::WriteLine (node->attributes[i ]->name);
Console::WriteLine (node->attributes[i ]->value);
}
}
Navigate (Node->firstchild, depth+1);
Navigate (node->nextsibling, depth);
}
void Main ()
{
XmlDocument ^doc = gcnew XmlDocument ();
Try
{
XmlReader ^reader = Xmlreader::create (".. Monsters.xml ");
Doc->load (reader);
Reader->close ();
XmlNode ^node = doc->firstchild;
Navigate (node, 0);
}
catch (Exception ^e)
{
Console::WriteLine ("error occurred: {0}", e->message);
}
}
Delete root node
#include "stdafx.h"
using namespace System;
using namespace System::xml;
using namespace System::xml::xpath;
void Navigate (XPathNavigator ^nav, int depth)
{
Console::WriteLine (depth);
Console::WriteLine (Nav->nodetype.tostring ());
Console::WriteLine (Nav->name);
Console::WriteLine (Nav->value);
if (nav->hasattributes)
{
Nav->movetofirstattribute ();
do {
Console::WriteLine (nav->name);
Console::WriteLine (nav->value);
}
while (Nav->movetonextattribute ());
nav->movetoparent ();
}
if (Nav->movetofirstchild ())
{
Navigate (NAV, depth+1);
nav->movetoparent ();
}
if (Nav->movetonext ())
Navigate (nav, depth );
}
Void Main ()
{
xmldocument ^doc = gcnew XmlDocument ();
try
{
doc->load (".. Monsters.xml ");
XPathNavigator ^nav = Doc->createnavigator ();
nav->movetoroot ();
Navigate (nav, 0);
}
catch (Exception ^e)
{
Console::WriteLine ("error occurred: {0}", e->message);
}
}