Export (0) Print
Expand All

XmlAttributes.XmlText Property

Gets or sets an object that instructs the XmlSerializer to serialize a public field or public read/write property as XML text.

Namespace:  System.Xml.Serialization
Assemblies:   System.Xml (in System.Xml.dll)
  System.Xml.XmlSerializer (in System.Xml.XmlSerializer.dll)

'Declaration
Public Property XmlText As XmlTextAttribute

Property Value

Type: System.Xml.Serialization.XmlTextAttribute
An XmlTextAttribute that overrides the default serialization of a public property or field.

By default, a public field or public read/write property is serialized as an XML element by the XmlSerializer. However, it can force the field or property to be serialized as XML text by applying an XmlTextAttribute to the field or property.

NoteNote

The XmlTextAttribute cannot be applied to a field or property that returns an array.

To override the default serialization of a field or property (that does not return an array), create an XmlTextAttribute and assign it to the XmlText property of an XmlAttributes object. Add the XmlAttributes object to an XmlAttributeOverrides object and specify the type of the object that contains the overridden field or property, and the name of the overridden field or property.

The following example serializes the class named Group, which contains a field named Comment. To override the default way the XmlSerializer serializes the field, the example creates an XmlAttributeOverrides and an XmlAttributes object. The example then creates an XmlTextAttribute object, which it assigns to the XmlText property, and adds the XmlAttributes object (with the name of the field to be serialized as XML text) to the XmlAttributeOverrides object. Lastly the example creates an XmlSerializer using the XmlAttributeOverrides object.

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


' This is the class that will be serialized. 
Public Class Group
    Public GroupName As String 

    ' This field will be serialized as XML text.  
    Public Comment As String 
End Class 


Public Class Run

    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("OverrideText.xml")
        test.DeserializeObject("OverrideText.xml")
    End Sub 


    ' Return an XmlSerializer to be used for overriding.  
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributeOverrides and XmlAttributes objects. 
        Dim xOver As New XmlAttributeOverrides()
        Dim xAttrs As New XmlAttributes()

        ' Create an XmlTextAttribute and assign it to the XmlText 
        ' property. This instructs the XmlSerializer to treat the 
        ' Comment field as XML text.  
        Dim xText As New XmlTextAttribute()
        xAttrs.XmlText = xText
        xOver.Add(GetType(Group), "Comment", xAttrs)

        ' Create the XmlSerializer, and return it. 
        Return New XmlSerializer(GetType(Group), xOver)
    End Function 



    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class. 
        Dim mySerializer As XmlSerializer = CreateOverrider()
        ' Writing the file requires a TextWriter. 
        Dim writer As New StreamWriter(filename)

        ' Create an instance of the class that will be serialized. 
        Dim myGroup As New Group()

        ' Set the object properties.
        myGroup.GroupName = ".NET"
        myGroup.Comment = "Great Stuff!" 
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub 


    Public Sub DeserializeObject(ByVal filename As String)
        Dim mySerializer As XmlSerializer = CreateOverrider()
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
        Console.WriteLine(myGroup.GroupName)
        Console.WriteLine(myGroup.Comment)
    End Sub 
End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft