Ce sujet n'a pas encore été évalué - Évaluez ce sujet

XmlTextAttribute, constructeur ()

Initialise une nouvelle instance de la classe XmlTextAttribute.

Espace de noms : System.Xml.Serialization
Assembly : System.Xml (dans system.xml.dll)

public XmlTextAttribute ()
public XmlTextAttribute ()
public function XmlTextAttribute ()

Vous pouvez substituer la manière dont XmlSerializer sérialise un champ public ou une propriété de lecture/écriture publique en créant XmlAttributes et en affectant XmlTextAttribute à sa propriété XmlText. Pour plus d'informations, consultez la classe XmlAttributeOverrides.

L'exemple suivant sérialise une classe qui contient un champ public nommé Comment. L'exemple applique XmlTextAttribute au champ pour qu'il ne soit pas sérialisé en tant qu'élément XML mais en tant que texte XML.

using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;


public class Group {
    public string GroupName;
    public string Comment;
}

public class Test {
    public static void Main() {
        Test t = new Test();
        t.SerializerOrder("TextOverride.xml");
    }
    /* Create an instance of the XmlSerializer class that overrides 
       the default way it serializes an object. */
    public XmlSerializer CreateOverrider() {
        /* Create instances of the XmlAttributes and 
        XmlAttributeOverrides classes. */
   
        XmlAttributes attrs = new XmlAttributes();

        XmlAttributeOverrides xOver = new XmlAttributeOverrides();
              
        /* Create an XmlTextAttribute to override the default 
        serialization process. */
        XmlTextAttribute xText = new XmlTextAttribute();
        attrs.XmlText = xText;

        // Add the XmlAttributes to the XmlAttributeOverrides.
        xOver.Add(typeof(Group), "Comment", attrs);

        // Create the XmlSerializer, and return it.
        XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
        return xSer;
    }

    public void SerializerOrder(string filename) {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = CreateOverrider();

        // Create the object and serialize it.
        Group myGroup = new Group();
        myGroup.Comment = "This is a great product.";
      
        TextWriter writer = new StreamWriter(filename);
        xSer.Serialize(writer, myGroup);
    }
}
   

import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.Schema.*;
import System.Xml.*;

public class Group
{
    public String groupName;
    public String comment;
} //Group

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.SerializerOrder("TextOverride.xml");
    } //main

    /* Create an instance of the XmlSerializer class that overrides 
       the default way it serializes an object. */
    public XmlSerializer CreateOverrider()
    {
        /* Create instances of the XmlAttributes and 
           XmlAttributeOverrides classes. */
        XmlAttributes attrs = new XmlAttributes();
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();

        /* Create an XmlTextAttribute to override the default 
           serialization process. */
        XmlTextAttribute xText = new XmlTextAttribute();
        attrs.set_XmlText(xText);

        // Add the XmlAttributes to the XmlAttributeOverrides.
        xOver.Add(Group.class.ToType(), "comment", attrs);

        // Create the XmlSerializer, and return it.
        XmlSerializer xSer = new XmlSerializer(Group.class.ToType(), xOver);
        return xSer;
    } //CreateOverrider

    public void SerializerOrder(String filename)
    {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = CreateOverrider();

        // Create the object and serialize it.
        Group myGroup = new Group();
        myGroup.comment = "This is a great product.";
        TextWriter writer = new StreamWriter(filename);
        xSer.Serialize(writer, myGroup);
    } //SerializerOrder
} //Test

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition

Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.

.NET Framework

Prise en charge dans : 2.0, 1.1, 1.0

.NET Compact Framework

Prise en charge dans : 2.0, 1.0
Cela vous a-t-il été utile ?
(1500 caractères restants)

Ajouts de la communauté

AJOUTER
© 2013 Microsoft. Tous droits réservés.