XmlWriter writes XML data out : XmlWriter « XML « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » XML » XmlWriterScreenshots 
XmlWriter writes XML data out
 

using System;
using System.IO;

using System.Text;
using System.Xml;
using System.Xml.Schema;

class XMLSchemaExamples
{
    public static void Main()
    {

        StringBuilder output = new StringBuilder();

        String xmlString =
                @"<?xml version='1.0'?>
                    <!-- This is a sample XML document -->
                    <Items>
                      <Item>test with a child element <more/> stuff</Item>
                    </Items>";
        using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
        {
            XmlWriterSettings ws = new XmlWriterSettings();
            ws.Indent = true;
            using (XmlWriter writer = XmlWriter.Create(output, ws))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            writer.WriteStartElement(reader.Name);
                            break;
                        case XmlNodeType.Text:
                            writer.WriteString(reader.Value);
                            break;
                        case XmlNodeType.XmlDeclaration:
                        case XmlNodeType.ProcessingInstruction:
                            writer.WriteProcessingInstruction(reader.Name, reader.Value);
                            break;
                        case XmlNodeType.Comment:
                            writer.WriteComment(reader.Value);
                            break;
                        case XmlNodeType.EndElement:
                            writer.WriteFullEndElement();
                            break;
                    }
                }

            }
        }
        Console.WriteLine(output.ToString());
    }
}

   
  
Related examples in the same category
1.Closes this stream and the underlying stream.
2.Creates a new XmlWriter instance using the specified stream.
3.Creates a new XmlWriter instance using the stream and XmlWriterSettings object.
4.Creates a new XmlWriter instance using the specified filename.
5.Creates a new XmlWriter instance using the specified TextWriter.
6.Creates a new XmlWriter instance using the TextWriter and XmlWriterSettings objects.
7.Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
8.Returns the closest prefix defined in the current namespace scope for the namespace URI.
9.Writes out all the attributes found at the current position in the XmlReader.
10.Writes an attribute with the specified local name, namespace URI, and value.
11.Encodes the specified binary bytes as Base64 and writes out the resulting text.
12.Encodes the specified binary bytes as BinHex and writes out the resulting text.
13.Writes out a block containing the specified text.
14.Closes one element and pops the corresponding namespace scope.
15.Copies everything from the reader to the writer
16.Copy everything from the XPathNavigator to writer.
17.Write a processing instruction: .
18.Writes text content.
19.Writes a DateTime value.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.