MarkupWriter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / System / Windows / Markup / Primitives / MarkupWriter.cs / 1 / MarkupWriter.cs

                            //------------------------------------------------------------------------ 
//
//  Microsoft Windows Client Platform
//  Copyright (c) Microsoft Corporation. All rights reserved.
// 
//  File:      MarkupWriter.cs
// 
//  Contents:  XAML writer 
//
//  Created:   04/28/2005 [....] 
//
//-----------------------------------------------------------------------

using System; 
using System.ComponentModel;
using System.Reflection; 
using System.Collections; 
using System.Collections.Generic;
using System.Diagnostics; 
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml; 
using System.Xml.Serialization;
using System.Windows.Markup; 
using MS.Internal; 

namespace System.Windows.Markup.Primitives 
{

    /// 
    /// Routines to write a tree of objects to XAML format 
    /// 
    public sealed class MarkupWriter : IDisposable 
    { 
        /// 
        /// Create an instance of a MarkupObject for the given object. 
        /// 
        /// 
        /// The instance of an object that will be treated as the root of a serialization tree
        ///  
        /// 
        /// A MarkupObject that allows iterating a tree of objects as a serializer 
        ///  
        public static MarkupObject GetMarkupObjectFor(object instance)
        { 
            if (instance == null)
                throw new ArgumentNullException("instance");
            XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(null);
            manager.XamlWriterMode = XamlWriterMode.Expression; 
            return new ElementMarkupObject(instance, manager);
        } 
 
        /// 
        /// Create an instance of a MarkupObject for the given object. 
        /// 
        /// 
        /// The instance of an object that will be treated as the root of a serialization tree
        ///  
        /// 
        /// The serialization manager to use. 
        ///  
        /// 
        /// A MarkupObject that allows iterating a tree of objects as a serializer 
        /// 
        public static MarkupObject GetMarkupObjectFor(object instance, XamlDesignerSerializationManager manager)
        {
            if (instance == null) 
                throw new ArgumentNullException("instance");
            if (manager == null) 
                throw new ArgumentNullException("manager"); 
            return new ElementMarkupObject(instance, manager);
        } 

        /// 
        /// Serialize the given instance to a XML writer.
        ///  
        /// 
        /// The writer to use to write the serialized image of the instance 
        ///  
        /// 
        /// The object instance to serialize 
        /// 
        internal static void SaveAsXml(XmlWriter writer, object instance)
        {
            if (writer == null) 
                throw new ArgumentNullException("writer");
 
            SaveAsXml(writer, GetMarkupObjectFor(instance)); 
        }
 
        /// 
        /// Serialize the given instance to a XML writer.
        /// 
        ///  
        /// The writer to use to write the serialized image of the instance
        ///  
        ///  
        /// The serialization manager to use.
        ///  
        /// 
        /// The object instance to serialize
        /// 
        internal static void SaveAsXml(XmlWriter writer, object instance, XamlDesignerSerializationManager manager) 
        {
            if (writer == null) 
                throw new ArgumentNullException("writer"); 
            if (manager == null)
                throw new ArgumentNullException("manager"); 

            manager.ClearXmlWriter();

            SaveAsXml(writer, GetMarkupObjectFor(instance, manager)); 
        }
 
        ///  
        /// Serialize the given item to an XML writer.
        ///  
        /// 
        /// The writer to use to writer the serialized image of the item
        /// 
        ///  
        /// The item to serialize. For example, the return value of GetMarkupObjectFor
        ///  
        internal static void SaveAsXml(XmlWriter writer, MarkupObject item) 
        {
            // Consider turning Debug.Assert's in the WriteItem into exceptions 
            // if this method is public.

            if (writer == null)
                throw new ArgumentNullException("writer"); 
            if (item == null)
                throw new ArgumentNullException("item"); 
 
            try
            { 
                using (MarkupWriter markupWriter = new MarkupWriter(writer))
                {
                    markupWriter.WriteItem(item);
                } 
            }
            finally 
            { 
                writer.Flush();
            } 
        }

        /// 
        /// Throws an exception if the given type cannot be serialized because it is 
        /// 1) not public
        /// 2) a nested-public 
        /// 3) a generic type 
        /// 
        ///  
        /// The type to be checked
        /// 
        internal static void VerifyTypeIsSerializable(Type type)
        { 
            // Check the type to make sure that it is not a nested type, that it is public, and that it is not generic
            if (type.IsNestedPublic) 
            { 
                throw new InvalidOperationException( SR.Get( SRID.MarkupWriter_CannotSerializeNestedPublictype, type.ToString() ));
            } 
            if (!type.IsPublic )
            {
                throw new InvalidOperationException( SR.Get( SRID.MarkupWriter_CannotSerializeNonPublictype, type.ToString() ));
            } 
            if (type.IsGenericType)
            { 
                throw new InvalidOperationException( SR.Get( SRID.MarkupWriter_CannotSerializeGenerictype, type.ToString() )); 
            }
        } 

        #region Internal Implementation
        internal MarkupWriter(XmlWriter writer)
        { 
            _writer = writer;
            _xmlTextWriter = writer as XmlTextWriter; 
        } 

        ///  
        /// Dispose method
        /// 
        public void Dispose()
        { 
            GC.SuppressFinalize(this);
        } 
 
        private bool RecordNamespaces(Scope scope, MarkupObject item, IValueSerializerContext context, bool
            lastWasString) 
        {

            // Ensure that there's an xmlns declaration generated for strings that are emitted as content.
            // For example,  should not bring in the System namespace from mscorlib via xmlns but, 
            //  should not bring in the System namespace from mscorlib via xmlns but, 
            // 

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK