public class XmlAttributes
|
The members of the XmlAttributes class correspond directly to a family of attribute classes that control serialization. For example, the XmlAttributes.XmlText property must be set to an XmlTextAttribute, which allows you to override serialization of a field or property by instructing the XmlSerializer to serialize the property value as XML text. For a complete list of attributes that control serialization, see the XmlSerializer.
For more details on using the XmlAttributeOverrides with the XmlAttributes class, see the conceptual topic at MSDN: overridingserializationofclasseswithxmlattributeoverridesclass.
using System; using System.IO; using System.Xml.Serialization; public class Orchestra { public Instrument[] Instruments; } public class Instrument { public string Name; } public class Brass:Instrument { public bool IsValved; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("Override.xml"); test.DeserializeObject("Override.xml"); } public void SerializeObject(string filename) { /* Each overridden field, property, or type requires an XmlAttributes object. */ XmlAttributes attrs = new XmlAttributes(); /* Create an XmlElementAttribute to override the field that returns Instrument objects. The overridden field returns Brass objects instead. */ XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = "Brass"; attr.Type = typeof(Brass); // Add the element to the collection of elements. attrs.XmlElements.Add(attr); // Create the XmlAttributeOverrides object. XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); /* Add the type of the class that contains the overridden member and the XmlAttributes to override it with to the XmlAttributeOverrides object. */ attrOverrides.Add(typeof(Orchestra), "Instruments", attrs); // Create the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create the object that will be serialized. Orchestra band = new Orchestra(); // Create an object of the derived type. Brass i = new Brass(); i.Name = "Trumpet"; i.IsValved = true; Instrument[] myInstruments = {i}; band.Instruments = myInstruments; // Serialize the object. s.Serialize(writer,band); writer.Close(); } public void DeserializeObject(string filename) { XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Create an XmlElementAttribute to override the Instrument. XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = "Brass"; attr.Type = typeof(Brass); // Add the element to the collection of elements. attrs.XmlElements.Add(attr); attrOverrides.Add(typeof(Orchestra), "Instruments", attrs); // Create the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides); FileStream fs = new FileStream(filename, FileMode.Open); Orchestra band = (Orchestra) s.Deserialize(fs); Console.WriteLine("Brass:"); /* The difference between deserializing the overridden XML document and serializing it is this: To read the derived object values, you must declare an object of the derived type (Brass), and cast the Instrument instance to it. */ Brass b; foreach(Instrument i in band.Instruments) { b = (Brass)i; Console.WriteLine( b.Name + "\n" + b.IsValved); } } }
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the XmlAttributes class. |
ctor #2 | Overloaded:.ctor(ICustomAttributeProvider provider) |
XmlAnyAttribute | Read-write Gets or sets the XmlAnyAttributeAttribute to override. |
XmlAnyElements | Read-only Gets the collection of XmlAnyElementAttribute objects to override. |
XmlArray | Read-write Gets or sets an object that specifies how the XmlSerializer serializes a public field or read/write property that returns an array. |
XmlArrayItems | Read-only Gets or sets a collection of objects that specify how the XmlSerializer serializes items inserted into an array returned by a public field or read/write property. |
XmlAttribute | Read-write Gets or sets an object that specifies how the XmlSerializer serializes a public field or public read/write property as an XML attribute. |
XmlChoiceIdentifier | Read-only Gets or sets an object that allows you to disambiguate between a set of choices. |
XmlDefaultValue | Read-write Gets or sets the default value of an XML element or attribute. |
XmlElements | Read-only Gets or sets a collection of objects that specify how the XmlSerializer serializes a public field or read/write property as an XML element. |
XmlEnum | Read-write Gets or sets an object that specifies how the XmlSerializer serializes an enumeration member. |
XmlIgnore | Read-write Gets or sets a value that specifies whether or not the XmlSerializer serializes a public field or public read/write property. |
Xmlns | Read-write |
XmlRoot | Read-write Gets or sets an object that specifies how the XmlSerializer serializes a class as an XML root element. |
XmlText | Read-write Gets or sets an object that instructs the XmlSerializer to serialize a public field or public read/write property as XML text. |
XmlType | Read-write Gets or sets an object that specifies how the XmlSerializer serializes a class to which the XmlTypeAttribute has been applied. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public XmlAttributes(); |
using System; using System.IO; using System.Xml.Serialization; public class Orchestra { public Instrument[] Instruments; } public class Instrument { public string Name; } public class Brass:Instrument { public bool IsValved; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("Override.xml"); test.DeserializeObject("Override.xml"); } public void SerializeObject(string filename) { /* Each overridden field, property, or type requires an XmlAttributes object. */ XmlAttributes attrs = new XmlAttributes(); /* Create an XmlElementAttribute to override the field that returns Instrument objects. The overridden field returns Brass objects instead. */ XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = "Brass"; attr.Type = typeof(Brass); // Add the element to the collection of elements. attrs.XmlElements.Add(attr); // Create the XmlAttributeOverrides object. XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); /* Add the type of the class that contains the overridden member and the XmlAttributes to override it with to the XmlAttributeOverrides object. */ attrOverrides.Add(typeof(Orchestra), "Instruments", attrs); // Create the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create the object that will be serialized. Orchestra band = new Orchestra(); // Create an object of the derived type. Brass i = new Brass(); i.Name = "Trumpet"; i.IsValved = true; Instrument[] myInstruments = {i}; band.Instruments = myInstruments; // Serialize the object. s.Serialize(writer,band); writer.Close(); } public void DeserializeObject(string filename) { XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Create an XmlElementAttribute to override the Instrument. XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = "Brass"; attr.Type = typeof(Brass); // Add the element to the collection of elements. attrs.XmlElements.Add(attr); attrOverrides.Add(typeof(Orchestra), "Instruments", attrs); // Create the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides); FileStream fs = new FileStream(filename, FileMode.Open); Orchestra band = (Orchestra) s.Deserialize(fs); Console.WriteLine("Brass:"); /* The difference between deserializing the overridden XML document and serializing it is this: To read the derived object values, you must declare an object of the derived type (Brass), and cast the Instrument instance to it. */ Brass b; foreach(Instrument i in band.Instruments) { b = (Brass)i; Console.WriteLine( b.Name + "\n" + b.IsValved); } } }
public XmlAttributes( |
provider
public XmlAnyAttributeAttribute XmlAnyAttribute {get; set;}
|
The XmlAttributes.XmlAnyAttribute property allows you to override the serialization of a member to function as a member to which the XmlAnyAttributeAttribute has been applied.
public XmlAnyElementAttributes XmlAnyElements {get;}
|
The XmlAttributes.XmlAnyElements property allows you to override the serialization of a member to function as a member to which the XmlAnyElementAttribute has been applied.
using System; using System.IO; using System.Xml.Serialization; using System.Xml; public class Group{ public string GroupName; [XmlAnyElement] public object[]Things; } public class Test{ static void Main(){ Test t = new Test(); // 1 Run this and create the XML document. // 2 Add new elements to the XML document. // 3 Comment out the new line, and uncomment // the DeserializeObject line to deserialize the // XML document and see unknown elements. t.SerializeObject("UnknownElements.xml"); // t.DeserializeObject("UnknownElements.xml"); } private void SerializeObject(string filename){ XmlSerializer ser = new XmlSerializer(typeof (Group)); TextWriter writer = new StreamWriter(filename); Group g = new Group(); g.GroupName = "MyGroup"; ser.Serialize(writer, g); writer.Close(); } private void DeserializeObject(string filename){ XmlSerializer ser = CreateOverrideSerializer(); // A FileStream is needed to read the XML document. FileStream fs = new FileStream(filename, FileMode.Open); Group g = (Group) ser.Deserialize(fs); fs.Close(); Console.WriteLine(g.GroupName); Console.WriteLine(g.Things.Length); foreach(XmlElement xelement in g.Things){ Console.WriteLine(xelement.Name + ": " + xelement.InnerXml); } } private XmlSerializer CreateOverrideSerializer(){ XmlAnyElementAttribute myAnyElement = new XmlAnyElementAttribute(); XmlAttributeOverrides xOverride = new XmlAttributeOverrides(); XmlAttributes xAtts = new XmlAttributes(); xAtts.XmlAnyElements.Add(myAnyElement); xOverride.Add(typeof(Group), "Things", xAtts); return new XmlSerializer(typeof(Group) , xOverride); } }
public XmlArrayAttribute XmlArray {get; set;}
|
In the default serialization, no attribute is applied to the member. When serialized, the array will be serialized as a nested sequence of XML elements with the XML element name of the nested sequence taken from the member name.
To control the serialization more precisely, apply an XmlArrayAttribute to the field or property. For example, you can change the generated XML element name from the default to a different name by setting the XmlArrayAttribute.ElementName property to a new value.
The XmlAttributes.XmlArray property allows you to override the default serialization, as well as the serialization controlled by applying an XmlArrayAttribute to the member. For example, you can change the XML element name generated from the default (the member identifier) to a new value. In addition, if you apply an XmlArrayAttribute to a member, it will be overridden by any XmlArrayAttribute that is assigned to the XmlAttributes.XmlArray property.
using System; using System.IO; using System.Xml; using System.Xml.Serialization; // This is the class that will be serialized. public class Group { // This field will be overridden. public Member [] Members; } public class Member { public string MemberName; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("OverrideArray.xml"); test.DeserializeObject("OverrideArray.xml"); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateOverrider() { // Creating XmlAttributeOverrides and XmlAttributes objects. XmlAttributeOverrides xOver = new XmlAttributeOverrides(); XmlAttributes xAttrs = new XmlAttributes(); // Add an override for the XmlArray. XmlArrayAttribute xArray = new XmlArrayAttribute("Staff"); xArray.Namespace = "http://www.cpandl.com"; xAttrs.XmlArray = xArray; xOver.Add(typeof(Group), "Members", xAttrs); // Create the XmlSerializer and return it. return new XmlSerializer(typeof(Group), xOver); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer mySerializer = CreateOverrider(); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create an instance of the class that will be serialized. Group myGroup = new Group(); // Set the object properties. Member m = new Member(); m.MemberName = "Paul"; myGroup.Members = new Member[1] {m}; // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.Close(); } public void DeserializeObject(string filename) { XmlSerializer mySerializer = CreateOverrider(); FileStream fs = new FileStream(filename, FileMode.Open); Group myGroup = (Group) mySerializer.Deserialize(fs); foreach(Member m in myGroup.Members) { Console.WriteLine(m.MemberName); } } }
public XmlArrayItemAttributes XmlArrayItems {get;}
|
using System; using System.IO; using System.Xml; using System.Xml.Serialization; // This is the class that will be serialized. public class Group { public Member [] Members; } public class Member { public string MemberName; } public class NewMember:Member { public int MemberID; } public class RetiredMember:NewMember { public DateTime RetireDate; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("OverrideArrayItem.xml"); test.DeserializeObject("OverrideArrayItem.xml"); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateOverrider() { // Create XmlAttributeOverrides and XmlAttributes objects. XmlAttributeOverrides xOver = new XmlAttributeOverrides(); XmlAttributes xAttrs = new XmlAttributes(); // Add an override for the XmlArrayItem. XmlArrayItemAttribute xArrayItem = new XmlArrayItemAttribute(typeof(NewMember)); xArrayItem.Namespace = "http://www.cpandl.com"; xAttrs.XmlArrayItems.Add(xArrayItem); // Add a second override. XmlArrayItemAttribute xArrayItem2 = new XmlArrayItemAttribute(typeof(RetiredMember)); xArrayItem2.Namespace = "http://www.cpandl.com"; xAttrs.XmlArrayItems.Add(xArrayItem2); // Add all overrides to XmlAttribueOverrides object. xOver.Add(typeof(Group), "Members", xAttrs); // Create the XmlSerializer and return it. return new XmlSerializer(typeof(Group), xOver); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer mySerializer = CreateOverrider(); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create an instance of the class that will be serialized. Group myGroup = new Group(); // Set the object properties. NewMember m = new NewMember(); m.MemberName = "Paul"; m.MemberID = 2; // Create a second member. RetiredMember m2 = new RetiredMember(); m2.MemberName = "Renaldo"; m2.MemberID = 23; m2.RetireDate = new DateTime(2000, 10,10); myGroup.Members = new Member[2] {m, m2}; // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.Close(); } public void DeserializeObject(string filename) { XmlSerializer mySerializer = CreateOverrider(); FileStream fs = new FileStream(filename, FileMode.Open); Group myGroup = (Group) mySerializer.Deserialize(fs); foreach(Member m in myGroup.Members) { Console.WriteLine(m.MemberName); } } }
public XmlAttributeAttribute XmlAttribute {get; set;}
|
using System; using System.IO; using System.Xml; using System.Xml.Serialization; // This is the class that will be serialized. public class Group { // This is the attribute that will be overridden. [XmlAttribute] public string GroupName; public int GroupNumber; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("OverrideAttribute.xml"); test.DeserializeObject("OverrideAttribute.xml"); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateOverrider() { // Create the XmlAttributeOverrides and XmlAttributes objects. XmlAttributeOverrides xOver = new XmlAttributeOverrides(); XmlAttributes xAttrs = new XmlAttributes(); /* Create an overriding XmlAttributeAttribute set it to the XmlAttribute property of the XmlAttributes object.*/ XmlAttributeAttribute xAttribute = new XmlAttributeAttribute("SplinterName"); xAttrs.XmlAttribute = xAttribute; // Add to the XmlAttributeOverrides. Specify the member name. xOver.Add(typeof(Group), "GroupName", xAttrs); // Create the XmlSerializer and return it. return new XmlSerializer(typeof(Group), xOver); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer mySerializer = CreateOverrider(); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create an instance of the class that will be serialized. Group myGroup = new Group(); /* Set the Name property, which will be generated as an XML attribute. */ myGroup.GroupName = ".NET"; myGroup.GroupNumber = 1; // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myGroup); writer.Close(); } public void DeserializeObject(string filename) { XmlSerializer mySerializer = CreateOverrider(); FileStream fs = new FileStream(filename, FileMode.Open); Group myGroup = (Group) mySerializer.Deserialize(fs); Console.WriteLine(myGroup.GroupName); Console.WriteLine(myGroup.GroupNumber); } }
public XmlChoiceIdentifierAttribute XmlChoiceIdentifier {get;}
|
public object XmlDefaultValue {get; set;}
|
<?xml version="1.0"?> <schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="" xmlns="http://www.w3.org/2000/10/XMLSchema"> <element name="Pets" nullable="true" type="Pets"/> <complexType name="Pets"> <sequence> <element default="Dogs" name="Animal" nullable="true" type="string" minOccurs="0"/> </sequence> </complexType> </schema>
To override the default value, create an Object and assign it to the XmlAttributes.XmlDefaultValue.
If the value assigned to a field or property is equal to the default value for that field or property, the XmlSerializer will not serialize the value to the XML-instance. This is because the assigned value can be recovered from the XML schema. In other words, setting a field or property to its own default value is equivalent of not setting it at all. Likewise, if no value is set for the field or property, the XmlSerializer will use the default value found in the schema. In both cases, (setting the property to its default, or not setting it at all), the XML-document instance will not contain any value for the property.
using System; using System.IO; using System.Xml; using System.Xml.Serialization; using System.ComponentModel; // This is the class that will be serialized. public class Pet { // The default value for the Animal field is "Dog". [DefaultValueAttribute("Dog")] public string Animal ; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("OverrideDefaultValue.xml"); test.DeserializeObject("OverrideDefaultValue.xml"); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateOverrider() { // Create the XmlAttributeOverrides and XmlAttributes objects. XmlAttributeOverrides xOver = new XmlAttributeOverrides(); XmlAttributes xAttrs = new XmlAttributes(); // Add an override for the default value of the GroupName. Object defaultAnimal= "Cat"; xAttrs.XmlDefaultValue = defaultAnimal; // Add all the XmlAttributes to the XmlAttribueOverrides object. xOver.Add(typeof(Pet), "Animal", xAttrs); // Create the XmlSerializer and return it. return new XmlSerializer(typeof(Pet), xOver); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer mySerializer = CreateOverrider(); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create an instance of the class that will be serialized. Pet myPet = new Pet(); /* Set the Animal property. If you set it to the default value, which is "Cat" (the value assigned to the XmlDefaultValue of the XmlAttributes object), no value will be serialized. If you change the value to any other value (including "Dog"), the value will be serialized. */ // The default value "Cat" will be assigned (nothing serialized). myPet.Animal= ""; // Uncommenting the next line also results in the default // value because Cat is the default value (not serialized). // myPet.Animal = "Cat"; // Uncomment the next line to see the value serialized: // myPet.Animal = "fish"; // This will also be serialized because Dog is not the // default anymore. // myPet.Animal = "Dog"; // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myPet); writer.Close(); } public void DeserializeObject(string filename) { XmlSerializer mySerializer = CreateOverrider(); FileStream fs = new FileStream(filename, FileMode.Open); Pet myPet= (Pet)mySerializer.Deserialize(fs); Console.WriteLine(myPet.Animal); } }
public XmlElementAttributes XmlElements {get;}
|
using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; public class Transportation { // Subsequent code overrides these two XmlElementAttributes. [XmlElement(typeof(Car)), XmlElement(typeof(Plane))] public ArrayList Vehicles; } public class Car { public string Name; } public class Plane { public string Name; } public class Truck { public string Name; } public class Train { public string Name; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("OverrideElement.xml"); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateOverrider() { // Create the XmlAttributes and XmlAttributeOverrides objects. XmlAttributes attrs = new XmlAttributes(); XmlAttributeOverrides xOver = new XmlAttributeOverrides(); /* Create an XmlElementAttribute to override the Vehicles property. */ XmlElementAttribute xElement1 = new XmlElementAttribute(typeof(Truck)); // Add the XmlElementAttribute to the collection. attrs.XmlElements.Add(xElement1); /* Create a second XmlElementAttribute, and add it to the collection. */ XmlElementAttribute xElement2 = new XmlElementAttribute(typeof(Train)); attrs.XmlElements.Add(xElement2); /* Add the XmlAttributes to the XmlAttributeOverrides, specifying the member to override. */ xOver.Add(typeof(Transportation), "Vehicles", attrs); // Create the XmlSerializer, and return it. XmlSerializer xSer = new XmlSerializer (typeof(Transportation), xOver); return xSer; } public void SerializeObject(string filename) { // Create an XmlSerializer instance. XmlSerializer xSer = CreateOverrider(); // Create the object and serialize it. Transportation myTransportation = new Transportation(); /* Create two new override objects that can be inserted into the array. */ myTransportation.Vehicles = new ArrayList(); Truck myTruck = new Truck(); myTruck.Name = "MyTruck"; Train myTrain = new Train(); myTrain.Name = "MyTrain"; myTransportation.Vehicles.Add(myTruck); myTransportation.Vehicles.Add(myTrain); TextWriter writer = new StreamWriter(filename); xSer.Serialize(writer, myTransportation); } }
public XmlEnumAttribute XmlEnum {get; set;}
|
using System; using System.IO; using System.Xml; using System.Xml.Serialization; // This is the class that will be serialized. public class Food { public FoodType Type; } public enum FoodType { // Subsequent code overrides these enumerations. Low, High } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("OverrideEnum.xml"); test.DeserializeObject("OverrideEnum.xml"); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateOverrider() { // Create the XmlAttributeOverrides and XmlAttributes objects. XmlAttributeOverrides xOver = new XmlAttributeOverrides(); XmlAttributes xAttrs = new XmlAttributes(); // Add an XmlEnumAttribute for the FoodType.Low enumeration. XmlEnumAttribute xEnum = new XmlEnumAttribute(); xEnum.Name = "Cold"; xAttrs.XmlEnum = xEnum; xOver.Add(typeof(FoodType), "Low", xAttrs); // Add an XmlEnumAttribute for the FoodType.High enumeration. xAttrs = new XmlAttributes(); xEnum = new XmlEnumAttribute(); xEnum.Name = "Hot"; xAttrs.XmlEnum = xEnum; xOver.Add(typeof(FoodType), "High", xAttrs); // Create the XmlSerializer, and return it. return new XmlSerializer(typeof(Food), xOver); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer mySerializer = CreateOverrider(); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create an instance of the class that will be serialized. Food myFood = new Food(); // Set the object properties. myFood.Type = FoodType.High; // Serialize the class, and close the TextWriter. mySerializer.Serialize(writer, myFood); writer.Close(); } public void DeserializeObject(string filename) { XmlSerializer mySerializer = CreateOverrider(); FileStream fs = new FileStream(filename, FileMode.Open); Food myFood = (Food) mySerializer.Deserialize(fs); Console.WriteLine(myFood.Type); } }
public bool XmlIgnore {get; set;}
|
To override the default serialization of a field or property, create an XmlAttributes object, and set its XmlAttributes.XmlIgnore property to true. XmlAttributeOverrides.Add the object to an XmlAttributeOverrides object specifying the type of the object that contains the field or property to ignore, and the name of the field or property to ignore.
If an XmlIgnoreAttribute is applied to a field or property, the field or property will be ignored. However you can override that behavior by creating an XmlAttributes object, setting its XmlAttributes.XmlIgnore property to false, and adding it to an XmlAttributeOverrides object specifying the type of the object that contains the field or property, and the name of the field or property.
using System; using System.IO; using System.Xml.Serialization; // This is the class that will be serialized. public class Group { // The GroupName value will be serialized--unless it's overridden. public string GroupName; /* This field will be ignored when serialized-- unless it's overridden. */ [XmlIgnoreAttribute] public string Comment; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("IgnoreXml.xml"); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateOverrider() { // Create the XmlAttributeOverrides and XmlAttributes objects. XmlAttributeOverrides xOver = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); /* Setting XmlIgnore to false overrides the XmlIgnoreAttribute applied to the Comment field. Thus it will be serialized.*/ attrs.XmlIgnore = false; xOver.Add(typeof(Group), "Comment", attrs); /* Use the XmlIgnore to instruct the XmlSerializer to ignore the GroupName instead. */ attrs = new XmlAttributes(); attrs.XmlIgnore = true; xOver.Add(typeof(Group), "GroupName", attrs); XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver); return xSer; } public void SerializeObject(string filename) { // Create an XmlSerializer instance. XmlSerializer xSer = CreateOverrider(); // Create the object to serialize and set its properties. Group myGroup = new Group(); myGroup.GroupName = ".NET"; myGroup.Comment = "My Comment..."; // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Serialize the object and close the TextWriter. xSer.Serialize(writer, myGroup); writer.Close(); } }
public bool Xmlns {get; set;}
|
public XmlRootAttribute XmlRoot {get; set;}
|
using System; using System.IO; using System.Xml.Serialization; // This is the class that will be serialized. public class Group { public string GroupName; [XmlAttribute] public int GroupCode; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("OverrideRoot.xml"); } // Return an XmlSerializer for overriding attributes. public XmlSerializer CreateOverrider() { // Create the XmlAttributes and XmlAttributeOverrides objects. XmlAttributes attrs = new XmlAttributes(); XmlAttributeOverrides xOver = new XmlAttributeOverrides(); XmlRootAttribute xRoot = new XmlRootAttribute(); // Set a new Namespace and ElementName for the root element. xRoot.Namespace = "http://www.cpandl.com"; xRoot.ElementName = "NewGroup"; attrs.XmlRoot = xRoot; /* Add the XmlAttributes object to the XmlAttributeOverrides. No member name is needed because the whole class is overridden. */ xOver.Add(typeof(Group), attrs); // Get the XmlAttributes object, based on the type. XmlAttributes tempAttrs; tempAttrs = xOver[typeof(Group)]; // Print the Namespace and ElementName of the root. Console.WriteLine(tempAttrs.XmlRoot.Namespace); Console.WriteLine(tempAttrs.XmlRoot.ElementName); XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver); return xSer; } public void SerializeObject(string filename) { // Create the XmlSerializer using the CreateOverrider method. XmlSerializer xSer = CreateOverrider(); // Create the object to serialize. Group myGroup = new Group(); myGroup.GroupName = ".NET"; myGroup.GroupCode = 123; // To write the file, a TextWriter is required. TextWriter writer = new StreamWriter(filename); // Serialize the object and close the TextWriter. xSer.Serialize(writer, myGroup); writer.Close(); } }
public XmlTextAttribute XmlText {get; set;}
|
To override the default serialization of a field or property (that does not return an array), create an XmlTextAttribute and assign it to the XmlAttributes.XmlText property of an XmlAttributes object. Add the XmlAttributes object to an XmlAttributeOverrides object, specifying the type of the object containing the overridden field or property, and the name of the overridden field or property.
using System; using System.IO; using System.Xml; using System.Xml.Serialization; // This is the class that will be serialized. public class Group { public string GroupName; // This field will be serialized as XML text. public string Comment; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("OverrideText.xml"); test.DeserializeObject("OverrideText.xml"); } // Return an XmlSerializer to be used for overriding. public XmlSerializer CreateOverrider() { // Create the XmlAttributeOverrides and XmlAttributes objects. XmlAttributeOverrides xOver = new XmlAttributeOverrides(); XmlAttributes xAttrs = new XmlAttributes(); /* Create an XmlTextAttribute and assign it to the XmlText property. This instructs the XmlSerializer to treat the Comment field as XML text. */ XmlTextAttribute xText = new XmlTextAttribute(); xAttrs.XmlText = xText; xOver.Add(typeof(Group), "Comment", xAttrs); // Create the XmlSerializer, and return it. return new XmlSerializer(typeof(Group), xOver); } public void SerializeObject(string filename) { // Create an instance of the XmlSerializer class. XmlSerializer mySerializer = CreateOverrider(); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Create an instance of the class that will be serialized. Group myGroup = 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(); } public void DeserializeObject(string filename) { XmlSerializer mySerializer = CreateOverrider(); FileStream fs = new FileStream(filename, FileMode.Open); Group myGroup = (Group) mySerializer.Deserialize(fs); Console.WriteLine(myGroup.GroupName); Console.WriteLine(myGroup.Comment); } }
public XmlTypeAttribute XmlType {get; set;}
|
using System; using System.IO; using System.Xml.Serialization; public class Transportation { public Car[] Cars; } public class Car { public int ID; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("XmlType.xml"); } // Return an XmlSerializer used for overriding. public XmlSerializer CreateOverrider() { // Create the XmlAttributes and XmlAttributeOverrides objects. XmlAttributes attrs = new XmlAttributes(); XmlAttributeOverrides xOver = new XmlAttributeOverrides(); /* Create an XmlTypeAttribute and change the name of the XML type. */ XmlTypeAttribute xType = new XmlTypeAttribute(); xType.TypeName = "Autos"; // Set the XmlTypeAttribute to the XmlType property. attrs.XmlType = xType; /* Add the XmlAttributes to the XmlAttributeOverrides, specifying the member to override. */ xOver.Add(typeof(Car), attrs); // Create the XmlSerializer, and return it. XmlSerializer xSer = new XmlSerializer (typeof(Transportation), xOver); return xSer; } public void SerializeObject(string filename) { // Create an XmlSerializer instance. XmlSerializer xSer = CreateOverrider(); // Create object and serialize it. Transportation myTransportation = new Transportation(); Car c1 = new Car(); c1.ID = 12; Car c2 = new Car(); c2.ID = 44; myTransportation.Cars = new Car[2]{c1,c2}; // To write the file, a TextWriter is required. TextWriter writer = new StreamWriter(filename); xSer.Serialize(writer, myTransportation); } }
~XmlAttributes(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual string ToString(); |