Visual C++ .NET/XML/XmlReader — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:05, 26 мая 2010

Does it have attribute

 

#include "stdafx.h"
#using <system.xml.dll>
using namespace System;
using namespace System::Xml;
void main()
{
    XmlReader ^reader;
    reader = XmlReader::Create("..\\Monsters.xml");
   while (reader->Read()){
            switch (reader->NodeType)
            {
                case XmlNodeType::Comment:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Value);
                    break;
                case XmlNodeType::Element:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    if (reader->HasAttributes)
                    {
                        while (reader->MoveToNextAttribute())
                        {
                            Console::WriteLine(reader->Depth);
                            Console::WriteLine(reader->Name);
                            Console::WriteLine(reader->Value);
                        }
                        reader->MoveToElement();
                    }
                    if (reader->IsEmptyElement)
                    {
                        Console::WriteLine(reader->Depth);
                        Console::WriteLine(reader->Name);
                    }
                    break;
                case XmlNodeType::EndElement:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    break;
                case XmlNodeType::Text:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Value);
                    break;
                case XmlNodeType::XmlDeclaration:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    if (reader->HasAttributes)
                    {
                        while (reader->MoveToNextAttribute())
                        {
                            Console::WriteLine(reader->Depth);
                            Console::WriteLine(reader->Name);
                            Console::WriteLine(reader->Value);
                        }
                    }
                    reader->MoveToElement();
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    break;
                case XmlNodeType::Whitespace:
                    break;
                default:
                    Console::WriteLine("***UKNOWN*** node");
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    Console::WriteLine(reader->Value);
            }
        }
}


Load xml from file

 

#include "stdafx.h"
#using <system.xml.dll>
using namespace System;
using namespace System::Xml;
void main()
{
    XmlReader ^reader;
    reader = XmlReader::Create("..\\Monsters.xml");
   while (reader->Read()){
            switch (reader->NodeType)
            {
                case XmlNodeType::Comment:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Value);
                    break;
                case XmlNodeType::Element:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    if (reader->HasAttributes)
                    {
                        while (reader->MoveToNextAttribute())
                        {
                            Console::WriteLine(reader->Depth);
                            Console::WriteLine(reader->Name);
                            Console::WriteLine(reader->Value);
                        }
                        reader->MoveToElement();
                    }
                    if (reader->IsEmptyElement)
                    {
                        Console::WriteLine(reader->Depth);
                        Console::WriteLine(reader->Name);
                    }
                    break;
                case XmlNodeType::EndElement:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    break;
                case XmlNodeType::Text:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Value);
                    break;
                case XmlNodeType::XmlDeclaration:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    if (reader->HasAttributes)
                    {
                        while (reader->MoveToNextAttribute())
                        {
                            Console::WriteLine(reader->Depth);
                            Console::WriteLine(reader->Name);
                            Console::WriteLine(reader->Value);
                        }
                    }
                    reader->MoveToElement();
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    break;
                case XmlNodeType::Whitespace:
                    break;
                default:
                    Console::WriteLine("***UKNOWN*** node");
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    Console::WriteLine(reader->Value);
            }
        }
}


Output depth of element

 

#include "stdafx.h"
#using <system.xml.dll>
using namespace System;
using namespace System::Xml;
void main()
{
    XmlReader ^reader;
    reader = XmlReader::Create("..\\Monsters.xml");
    while (reader->Read()){
            switch (reader->NodeType)
            {
                case XmlNodeType::Comment:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Value);
                    break;
                case XmlNodeType::Element:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    if (reader->HasAttributes)
                    {
                        while (reader->MoveToNextAttribute())
                        {
                            Console::WriteLine(reader->Depth);
                            Console::WriteLine(reader->Name);
                            Console::WriteLine(reader->Value);
                        }
                        reader->MoveToElement();
                    }
                    if (reader->IsEmptyElement)
                    {
                        Console::WriteLine(reader->Depth);
                        Console::WriteLine(reader->Name);
                    }
                    break;
                case XmlNodeType::EndElement:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    break;
                case XmlNodeType::Text:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Value);
                    break;
                case XmlNodeType::XmlDeclaration:
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    if (reader->HasAttributes)
                    {
                        while (reader->MoveToNextAttribute())
                        {
                            Console::WriteLine(reader->Depth);
                            Console::WriteLine(reader->Name);
                            Console::WriteLine(reader->Value);
                        }
                    }
                    reader->MoveToElement();
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    break;
                case XmlNodeType::Whitespace:
                    break;
                default:
                    Console::WriteLine("***UKNOWN*** node");
                    Console::WriteLine(reader->Depth);
                    Console::WriteLine(reader->Name);
                    Console::WriteLine(reader->Value);
            }
        }
}


Set indent char for xml output

 
#include "stdafx.h"
#using <system.xml.dll>
using namespace System;
using namespace System::Xml;
void main()
{
    XmlReader ^reader;
    XmlWriter ^writer;
    try
    {
        reader = XmlReader::Create("a.xml");
        XmlWriterSettings ^settings = gcnew XmlWriterSettings();
        settings->Indent = true;
        settings->IndentChars = ("   ");
        writer = XmlWriter::Create("New.xml", settings);
        while (reader->Read())
        {
            switch (reader->NodeType)
            {
                case XmlNodeType::Comment:
                    writer->WriteComment(reader->Value);
                    break;
                case XmlNodeType::Element:
                    writer->WriteStartElement(reader->Name);
                    writer->WriteAttributes(reader, false);
                    if (reader->IsEmptyElement)
                        writer->WriteEndElement();
                   break;
                case XmlNodeType::EndElement:
                    writer->WriteEndElement();
                    if (reader->Name->Equals("Name"))
                    {
                        writer->WriteStartElement("A");
                        writer->WriteString("False");
                        writer->WriteEndElement();
                    }
                    break;
                case XmlNodeType::Text:
                    writer->WriteString(reader->Value);
                    break;
                case XmlNodeType::XmlDeclaration:
                    writer->WriteStartDocument();
                    break;
            }
        }
        writer->Flush();
    }
    catch (Exception ^e)
    {
        Console::WriteLine("XML Update Aborted -- {0}", e->Message);
    }
    finally
    {
        if (writer->WriteState != WriteState::Closed)
        {
            writer->Close();
        }
        if (reader->ReadState != ReadState::Closed)
        {
            reader->Close();
        }
    }
}


Validate XML

 
#include "stdafx.h"
#using <system.xml.dll>
using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
ref class ValidateXML
{
public:
    ValidateXML(String ^filename)
    {
        XmlReader ^vreader;
        try
        {
            XmlReaderSettings ^settings = gcnew XmlReaderSettings();
            settings->ProhibitDtd = false;
            settings->ValidationType = ValidationType::DTD;
            vreader = XmlReader::Create(filename, settings);
     
            while(vreader->Read())
            {
                // ... Process nodes just like XmlTextReader()
            }
            Console::WriteLine("Finished Processing");
        }
        catch (Exception ^e)
        {
            Console::WriteLine(e->Message);
        }
        finally
        {
            if (vreader->ReadState != ReadState::Closed)
            {
                vreader->Close();
            }
        }
    }
};
void main()
{
    gcnew ValidateXML("Bad.xml");
    gcnew ValidateXML("good.xml"); 
}