Csharp/C Sharp by API/System.Xml.Serialization/XmlSerializer

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

new XmlSerializer

<source lang="csharp"> using System; using System.Xml; using System.Xml.Serialization; using System.IO; public class SerializeXml {

   private static void Main() {
       CarList catalog = new CarList("New List", DateTime.Now.AddYears(1));
       Car[] cars = new Car[2];
       cars[0] = new Car("Car 1", 12342.99m);
       cars[1] = new Car("Car 2", 21234123.9m);
       catalog.Cars = cars;
       XmlSerializer serializer = new XmlSerializer(typeof(CarList));
       FileStream fs = new FileStream("CarList.xml", FileMode.Create);
       serializer.Serialize(fs, catalog);
       fs.Close();
       catalog = null;
       // Deserialize the order from the file.
       fs = new FileStream("CarList.xml", FileMode.Open);
       catalog = (CarList)serializer.Deserialize(fs);
       // Serialize the order to the Console window.
       serializer.Serialize(Console.Out, catalog);
   }

}

[XmlRoot("carList")] public class CarList {

   [XmlElement("catalogName")]
   public string ListName;
   
   // Use the date data type (and ignore the time portion in the serialized XML).
   [XmlElement(ElementName="expiryDate", DataType="date")]
   public DateTime ExpiryDate;
   
   [XmlArray("cars")]
   [XmlArrayItem("car")]
   public Car[] Cars;
   public CarList() {
   }
   public CarList(string catalogName, DateTime expiryDate) {
       this.ListName = catalogName;
       this.ExpiryDate = expiryDate;
   }

} public class Car {

   [XmlElement("carName")]
   public string CarName;
   
   [XmlElement("carPrice")]
   public decimal CarPrice;
   
   [XmlElement("inStock")]
   public bool InStock;
   
   [XmlAttributeAttribute(AttributeName="id", DataType="integer")]
   public string Id;
   public Car() {
   }
   public Car(string carName, decimal carPrice) {
       this.CarName = carName;
       this.CarPrice = carPrice;
   }

}


 </source>


XmlSerializer.Deserialize

<source lang="csharp"> using System; using System.Xml; using System.Xml.Serialization; using System.IO; public class MainClass {

 private static void Main()
 {
   // Create the product catalog.
   ProductCatalog catalog = new ProductCatalog("New Catalog", DateTime.Now.AddYears(1));
   Product[] products = new Product[2];
   products[0] = new Product("Product 1", 42.99m);
   products[1] = new Product("Product 2", 202.99m);
   catalog.Products = products;
   // Serialize the order to a file.
   XmlSerializer serializer = new XmlSerializer(typeof(ProductCatalog));
   FileStream fs = new FileStream("ProductCatalog.xml", FileMode.Create);
   serializer.Serialize(fs, catalog);
   fs.Close();
   catalog = null;
   fs = new FileStream("ProductCatalog.xml", FileMode.Open);
   catalog = (ProductCatalog)serializer.Deserialize(fs);
   serializer.Serialize(Console.Out, catalog);
 }

} [XmlRoot("productCatalog")] public class ProductCatalog {

 [XmlElement("catalogName")]
 public string CatalogName;
 [XmlElement(ElementName="expiryDate", DataType="date")]
 public DateTime ExpiryDate;
 [XmlArray("products")]
 [XmlArrayItem("product")]
   public Product[] Products;
 public ProductCatalog()
 {
 }
 public ProductCatalog(string catalogName, DateTime expiryDate)
 {
   this.CatalogName = catalogName;
   this.ExpiryDate = expiryDate;
 }

}

public class Product {

 [XmlElement("productName")]
 public string ProductName;
 [XmlElement("productPrice")]
 public decimal ProductPrice;
 [XmlElement("inStock")]
 public bool InStock;
 [XmlAttributeAttribute(AttributeName="id", DataType="integer")]
 public string Id;
 public Product()
 {
 }
 public Product(string productName, decimal productPrice)
 {
   this.ProductName = productName;
   this.ProductPrice = productPrice;
 }

}


 </source>


XmlSerializer.Serialize

<source lang="csharp"> using System; using System.Collections; using System.Data; using System.IO; using System.Xml; using System.Xml.Serialization; class MainClass{

 public static void Main(){
    try {
        ShapedRoot sr = new ShapedRoot();
        sr.node = new Shaped();
        sr.node.s1 = "a value";
        sr.node.s2 = "another value";
        sr.node.s3 = "uuid:1AE0964A-2B30-4a02-96F2-325B92B4E92C";
        StringWriter sw = new StringWriter();
        XmlTextWriter tw = new XmlTextWriter( sw );
        tw.Formatting = Formatting.Indented;
        tw.Indentation = 4;
        XmlSerializer ser = new XmlSerializer( typeof( ShapedRoot ) );
        ser.Serialize( tw, sr );
        tw.Close();
        sw.Close();
    }
    catch( Exception exc )
    {
        Console.WriteLine( exc.Message );
    }

 }

} [XmlRoot( ElementName="sroot", Namespace="urn:my-examples:shaping" )] public class ShapedRoot {

   public ShapedRoot()
   {
   }
   public Shaped node;

} [XmlType( Namespace="urn:my-examples:shaping" )] public class Shaped {

   public Shaped()
   {
   }
   [XmlAttribute]
   public string s1;
   [XmlElement( ElementName = "string2" )]
   public string s2;
   [XmlElement( DataType = "anyURI" )]
   public string s3;

}


 </source>