using System.Xml.Serialization; using System.IO; public class Person { [XmlAttribute] public string Name { get; set; } [XmlAttribute] public int Age { get; set; } } // ... var person = new Person { Name = "John", Age = 30 }; var serializer = new XmlSerializer(typeof(Person)); using (var file = new StreamWriter("person.xml")) { serializer.WriteAttribute(file, person); }
using System.Xml.Serialization; using System.IO; public class Person { [XmlAttribute] public string Name { get; set; } [XmlAttribute] public int Age { get; set; } } // ... var person = new Person { Name = "John", Age = 30 }; var serializer = new XmlSerializer(typeof(Person)); using (var stream = new MemoryStream()) { serializer.WriteAttribute(stream, person); }In both examples, we define a `Person` class with two attributes (`Name` and `Age`) that we want to serialize. We create a new instance of `Person`, and then create an instance of `XmlSerializer`. In the first example, we write the serialized attributes to a file using a `StreamWriter`. In the second example, we write the serialized attributes to a `MemoryStream`. Both examples use the `WriteAttribute` method of the `XmlSerializer` object to write the serialized attributes to the file/stream.