XmlSerializer.Deserialize 方法 (Stream) 反序列化

//是将xml文件转化为实体对象的过程
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)); // Reading the XML document requires a FileStream. Stream reader= new FileStream(filename,FileMode.Open); // Declare an object variable of the type to be deserialized. OrderedItem i; // 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); } }
<?xml version="1.0"?>
 <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
   <inventory:ItemName>Widget</inventory:ItemName>
   <inventory:Description>Regular Widget</inventory:Description>
   <money:UnitPrice>2.3</money:UnitPrice>
   <inventory:Quantity>10</inventory:Quantity>
   <money:LineTotal>23</money:LineTotal>
 </OrderedItem>

 

转载于:https://www.cnblogs.com/xust/articles/2997527.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值