Visual C++ .NET/XML/DOM

Материал из .Net Framework эксперт
Версия от 12:05, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Recursive navigation of the DOM tree

 
#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++)
        {
            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);
    }
}


Update XML DOM

 
#include "stdafx.h"
using namespace System;
using namespace System::Xml;
void Navigate(XmlNode ^node)
{
    if (node == nullptr)
        return;
    if (node->Value != nullptr && node->Value->Equals("D"))
    {
        if (node->ParentNode->ParentNode["Name"]->FirstChild->Value->Equals("G"))
        {
            node->Value = "S";
            node->ParentNode->Attributes["Damage"]->Value = "1d8";
        }
    }
    Navigate(node->FirstChild);
    Navigate(node->NextSibling);
}

void main()
{
    XmlDocument ^doc = gcnew XmlDocument();
    try{
        doc->Load("a.xml");
        XmlNode ^root = doc->DocumentElement; 
        Navigate(root);
        doc->Save("New.xml");
    }catch (Exception ^e){
        Console::WriteLine("Error Occurred: {0}", e->Message );
    }
}


Write XML DOM

 
#include "stdafx.h"
using namespace System;
using namespace System::Xml;
XmlElement ^CreateMonster(XmlDocument ^doc){
    XmlElement ^skeleton = doc->CreateElement("M");
    XmlElement ^name = doc->CreateElement("N");
    name->AppendChild(doc->CreateTextNode("S"));
    skeleton->AppendChild(name);
    XmlElement ^hitdice = doc->CreateElement("H");
    XmlAttribute ^att = doc->CreateAttribute("D");
    att->Value = "1";
    hitdice->Attributes->Append(att);
    att = doc->CreateAttribute("Default");
    att->Value = "3";
    hitdice->Attributes->Append(att);
    skeleton->AppendChild(hitdice);
    XmlElement ^weapon = doc->CreateElement("W");
    att = doc->CreateAttribute("N");
    att->Value = "2";
    weapon->Attributes->Append(att);
    att = doc->CreateAttribute("D");
    att->Value = "1";
    weapon->Attributes->Append(att);
    weapon->AppendChild(doc->CreateTextNode("C"));
    skeleton->AppendChild(weapon);
    return skeleton;
}
void main()
{
    XmlDocument ^doc = gcnew XmlDocument();
    try
    {
        doc->Load("a.xml");
        XmlNode ^root = doc->DocumentElement; 
        XmlNode ^child = root->FirstChild->NextSibling;
        root->InsertAfter(CreateMonster(doc), child);
 
        doc->Save("New.xml");
    }
    catch (Exception ^e)
    {
        Console::WriteLine("Error Occurred: {0}", e->Message );
    }
}