当前位置: 首页>>代码示例>>C#>>正文


C# XmlSerializer.Deserialize方法代码示例

本文整理汇总了C#中System.Xml.Serialization.XmlSerializer.Deserialize方法的典型用法代码示例。如果您正苦于以下问题:C# XmlSerializer.Deserialize方法的具体用法?C# XmlSerializer.Deserialize怎么用?C# XmlSerializer.Deserialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Xml.Serialization.XmlSerializer的用法示例。


在下文中一共展示了XmlSerializer.Deserialize方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Calculate

//引入命名空间
using System;
using System.IO;
using System.Xml.Serialization;

// This is the class that will be deserialized.
public class OrderedItem
{
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string ItemName;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string Description;
    [XmlElement(Namespace="http://www.cohowinery.com")]
    public decimal UnitPrice;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public int Quantity;
    [XmlElement(Namespace="http://www.cohowinery.com")]
    public decimal LineTotal;
    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}
 
public class Test
{
    public static void Main()
    {
        Test t = new Test();
        // Read a purchase order.
        t.DeserializeObject("simple.xml");
    }

    private void DeserializeObject(string filename)
    {   
        Console.WriteLine("Reading with Stream");
        // Create an instance of the XmlSerializer.
        XmlSerializer serializer = 
        new XmlSerializer(typeof(OrderedItem));
          
        // Declare an object variable of the type to be deserialized.
        OrderedItem i;

        using (Stream reader = new FileStream(filename, FileMode.Open))
        {
            // Call the Deserialize method to restore the object's state.
            i = (OrderedItem)serializer.Deserialize(reader);          
        }

        // Write out the properties of the object.
        Console.Write(
        i.ItemName + "\t" +
        i.Description + "\t" +
        i.UnitPrice + "\t" +
        i.Quantity + "\t" +
        i.LineTotal);
    }
}
开发者ID:.NET开发者,项目名称:System.Xml.Serialization,代码行数:59,代码来源:XmlSerializer.Deserialize

示例2: Calculate

//引入命名空间
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

// This is the class that will be deserialized.
public class OrderedItem
{
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string ItemName;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string Description;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal UnitPrice;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public int Quantity;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal LineTotal;
    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}
 
public class Test
{
   public static void Main()
    {
        Test t = new Test();
        // Read a purchase order.
        t.DeserializeObject("simple.xml");
    }

    private void DeserializeObject(string filename)
    {   
        Console.WriteLine("Reading with TextReader");

        // Create an instance of the XmlSerializer specifying type.
        XmlSerializer serializer = 
        new XmlSerializer(typeof(OrderedItem));

        // Create a TextReader to read the file. 
        FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
        TextReader reader = new StreamReader(fs);
      
        // Declare an object variable of the type to be deserialized.
        OrderedItem i;

        // Use the Deserialize method to restore the object's state.
        i = (OrderedItem) serializer.Deserialize(reader);

        // Write out the properties of the object.
        Console.Write(
            i.ItemName + "\t" +
            i.Description + "\t" +
            i.UnitPrice + "\t" +
            i.Quantity + "\t" +
            i.LineTotal);
    }
}
开发者ID:.NET开发者,项目名称:System.Xml.Serialization,代码行数:62,代码来源:XmlSerializer.Deserialize

示例3: Calculate

//引入命名空间
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be deserialized.
public class OrderedItem
{
    public string ItemName;
    public string Description;
    public decimal UnitPrice;
    public int Quantity;
    public decimal LineTotal;

    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}
public class Test
{
    public static void Main(string[] args)
    {
        Test t = new Test();
        // Read a purchase order.
        t.DeserializeObject("simple.xml");
    }

    private void DeserializeObject(string filename)
    {
        Console.WriteLine("Reading with XmlReader");

        // Create an instance of the XmlSerializer specifying type and namespace.
        XmlSerializer serializer = new
        XmlSerializer(typeof(OrderedItem));

        // A FileStream is needed to read the XML document.
        FileStream fs = new FileStream(filename, FileMode.Open);
        XmlReader reader = XmlReader.Create(fs);

        // Declare an object variable of the type to be deserialized.
        OrderedItem i;

        // Use the Deserialize method to restore the object's state.
        i = (OrderedItem)serializer.Deserialize(reader);
        fs.Close();

        // Write out the properties of the object.
        Console.Write(
        i.ItemName + "\t" +
        i.Description + "\t" +
        i.UnitPrice + "\t" +
        i.Quantity + "\t" +
        i.LineTotal);
    }
}
开发者ID:.NET开发者,项目名称:System.Xml.Serialization,代码行数:59,代码来源:XmlSerializer.Deserialize

示例4: Main

//引入命名空间
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;
  }
}
开发者ID:C#程序员,项目名称:System.Xml.Serialization,代码行数:81,代码来源:XmlSerializer.Deserialize


注:本文中的System.Xml.Serialization.XmlSerializer.Deserialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。