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

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

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

Output xml start element and end element

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


Output xml with indent

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


Update XML

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


Write XML out

 
#include "stdafx.h"
#using <system.xml.dll>
using namespace System;
using namespace System::Xml;
void main()
{
    XmlWriter ^writer;
    try
    {
        XmlWriterSettings ^settings = gcnew XmlWriterSettings();
        settings->Indent = true;
        settings->IndentChars = ("   ");
        settings->NewLineOnAttributes = true;
        writer = XmlWriter::Create("G.xml", settings);
        writer->WriteStartDocument();
        writer->WriteStartElement("M");
        writer->WriteComment("P");
        writer->WriteStartElement("M");
        writer->WriteStartElement("N");
        writer->WriteString("G");
        writer->WriteEndElement();
        writer->WriteStartElement("H");
        writer->WriteAttributeString("D", "1d8");
        writer->WriteAttributeString("D1", "4");
        writer->WriteEndElement();
        writer->WriteEndDocument();
        writer->Flush();
    }
    catch (Exception ^e)
    {
        Console::WriteLine("XML Writer Aborted -- {0}", e->Message);
    }
    finally
    {
        if (writer->WriteState != WriteState::Closed)
        {
            writer->Close();
        }
    }
}