2012-06-15 47 views
0

我有此xmlxmldeserialization沒有給出結果

<products> 
    <product> 
    <name>Demo</name> 
    </product> 
    <product> 
    <name>Black Beauty III</name> 
    <description>Beautiful les paul guitar</description> 
    <company> 
     <name>Les Paul</name> 
     <description>Very Good Company for guitar</description> 
     <year>1967</year> 
     <address> 
     <houseno>Flat no-52</houseno> 
     <street>Avishkar Appt. JVLR, Jogeshwari(E)</street> 
     <city>Mumbai</city> 
     <country>India</country> 
     <pin>400060</pin> 
     </address> 
    </company> 
    <grossprice>5700</grossprice> 
    <netprice>6000</netprice> 
    <measure>1 pc</measure> 
    <category>4</category> 
    </product> 
    <product> 
    <name></name> 
    <description></description> 
    <company> 
     <name></name> 
     <description></description> 
     <year></year> 
     <address> 
     <houseno></houseno> 
     <street></street> 
     <city></city> 
     <country></country> 
     <pin></pin> 
     </address> 
    </company> 
    <grossprice></grossprice> 
    <netprice></netprice> 
    <measure></measure> 
    <category></category> 
    </product> 
</products> 

通過使用此代碼我不能反序列化到對象

using (FileStream fs = new FileStream(System.IO.Path.Combine(System.Environment.CurrentDirectory + "/Products.xml"), FileMode.Open)) 
       { 
        XmlSerializer serializer = new XmlSerializer(typeof(Products)); 

        var products = serializer.Deserialize(fs) as Products; 
       } 

我在類中提到的所有的XML元素和XMLroots等

[XmlType("products")] 
[XmlInclude(typeof(Products))] 
public class Products 
{ 
    public Products() { } 

    public Products(Boolean GetProducts) 
    { 
     if (GetProducts) 
      Items = GetAllProducts().Items; 
    } 

    [XmlElement("product")] 
    public List<Product> Items { get; set; } 

    public Products GetAllProducts() 
    { 
     try 
     { 
      Products allProducts = new Products(); 
      using (FileStream fs = new FileStream(System.IO.Path.Combine(System.Environment.CurrentDirectory + "/Products.xml"), FileMode.Open)) 
      { 
       XmlSerializer serializer = new XmlSerializer(typeof(Products)); 

       var products = serializer.Deserialize(fs) as Products; 
      } 
      return allProducts; 
     } 
     catch (Exception ex) { return null; } 
    } 

    public Products GetAllProducts(ProductCategory Category) 
    { 
     return null; 
    } 
} 

[XmlType("product")] 
[XmlInclude(typeof(Product))] 
public class Product 
{ 
    [XmlElement("name")] 
    String Name { get; set; } 

    [XmlElement("description")] 
    String Description { get; set; } 

    [XmlElement("company")] 
    Company Brand { get; set; } 

    [XmlElement("grossprice")] 
    String GrossPrice { get; set; } 

    [XmlElement("netprice")] 
    String NetPrice { get; set; } 

    [XmlElement("measure")] 
    String Measure { get; set; } 

    [XmlElement("categoy")] 
    ProductCategory Category { get; set; } 

} 

如果你想我可以發佈更多的類結構...`在這裏輸入代碼

+0

結果爲空,還是僅僅是一個實例化的Products對象? catch塊是否被序列化異常擊中?如果是這樣,基本的異常消息是什麼? –

+0

沒有例外..它是空..沒有初始化類對象.. – 1Mayur

+0

Noe當我修改代碼..它顯示數據爲空,如何閱讀計數...就像我有兩個產品,所以它顯示計數爲2 – 1Mayur

回答

2

在您的課堂上公開您的物業。如果它們是私有的,受保護的或內部的,那麼XmlSerializer就不能看到,因此不能設置它們。

4

我得到了它使用的工作:

class Program 
    { 
     static void Main(string[] args) 
     { 
      using (FileStream fs = new FileStream("xml.xml", FileMode.Open)) 
      { 
       XmlSerializer serializer = new XmlSerializer(typeof(Products)); 

       var products = serializer.Deserialize(fs) as Products; 
      } 
     } 
    } 
    [XmlType("products")] 
    [XmlInclude(typeof(Products))] 
    public class Products 
    { 
     [XmlElement("product")] 
     public List<Product> Items { get; set; } 
    } 
    [XmlType("product")] 
    [XmlInclude(typeof(Product))] 
    public class Product 
    { 
     [XmlElement("name")] 
     String Name { get; set; } 

     [XmlElement("description")] 
     String Description { get; set; } 

     [XmlElement("company")] 
     Company Brand { get; set; } 

     [XmlElement("grossprice")] 
     String GrossPrice { get; set; } 

     [XmlElement("netprice")] 
     String NetPrice { get; set; } 

     [XmlElement("measure")] 
     String Measure { get; set; } 

     [XmlElement("categoy")] 
     ProductCategory Category { get; set; } 
    } 
    [XmlType("company")] 
    [XmlInclude(typeof(Company))] 
    public class Company 
    { 
     [XmlElement("name")] 
     public string Name { get; set; } 
     [XmlElement("description")] 
     public string Description { get; set; } 
     [XmlElement("year")] 
     public string Year { get; set; } 
     [XmlElement("address")] 
     public Address Address { get; set; } 

     public Company() 
     { 

     } 
    } 
    [XmlType("address")] 
    [XmlInclude(typeof(Address))] 
    public class Address 
    { 
     [XmlElement("houseno")] 
     public string HouseNumber { get; set; } 
     [XmlElement("street")] 
     public string Street { get; set; } 
     [XmlElement("city")] 
     public string City { get; set; } 
     [XmlElement("country")] 
     public string Country { get; set; } 
     [XmlElement("pin")] 
     public string Pin { get; set; } 

     public Address() 
     { 

     } 
    } 
    [XmlType("category")] 
    [XmlInclude(typeof(ProductCategory))] 
    public class ProductCategory 
    { 
     public ProductCategory() 
     { 

     } 
    } 

當你把一切都變成使用正確的XML屬性標記的對象這是最簡單的。

+0

不工作...顯示空白..然而變量正在初始化,但每一個我得到空和0 – 1Mayur

+0

productcategory是枚舉 – 1Mayur

+0

我得到了答案,我也必須使類的屬性產品也作爲公衆 – 1Mayur