2010-03-24 59 views
0

我有一個反序列化的xml c#objet。 我需要遍歷該對象來顯示所有項目,在這種情況下只有一個項目,並顯示每個項目的名稱,顏色和大小。通過反序列化的xml對象迭代

的XML:

<?xml version="1.0" encoding="utf-8"?> 
<Catalog Name="Example"> 
    <Items> 
    <Item Name="ExampleItem"> 
     <Colors> 
     <Color Name="Black" Value="#000" /> 
     <Color Name="White" Value="#FFF" /> 
     </Colors> 
     <Sizes> 
     <Size Name="Small" Value="10" /> 
     <Size Name="Medium" Value="20" /> 
     </Sizes> 
    </Item> 
    </Items> 
</Catalog> 

XSD.EXE生成的類:

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:2.0.50727.4927 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

using System.Xml.Serialization; 

// 
// This source code was auto-generated by xsd, Version=2.0.50727.42. 
// 


/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
public partial class Catalog { 

    private CatalogItemsItem[][] itemsField; 

    private string nameField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    [System.Xml.Serialization.XmlArrayItemAttribute("Item", typeof(CatalogItemsItem[]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] 
    public CatalogItemsItem[][] Items { 
     get { 
      return this.itemsField; 
     } 
     set { 
      this.itemsField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name { 
     get { 
      return this.nameField; 
     } 
     set { 
      this.nameField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
public partial class CatalogItemsItem { 

    private CatalogItemsItemColorsColor[][] colorsField; 

    private CatalogItemsItemSizesSize[][] sizesField; 

    private string nameField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    [System.Xml.Serialization.XmlArrayItemAttribute("Color", typeof(CatalogItemsItemColorsColor[]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] 
    public CatalogItemsItemColorsColor[][] Colors { 
     get { 
      return this.colorsField; 
     } 
     set { 
      this.colorsField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    [System.Xml.Serialization.XmlArrayItemAttribute("Size", typeof(CatalogItemsItemSizesSize[]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] 
    public CatalogItemsItemSizesSize[][] Sizes { 
     get { 
      return this.sizesField; 
     } 
     set { 
      this.sizesField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name { 
     get { 
      return this.nameField; 
     } 
     set { 
      this.nameField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
public partial class CatalogItemsItemColorsColor { 

    private string nameField; 

    private string valueField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name { 
     get { 
      return this.nameField; 
     } 
     set { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Value { 
     get { 
      return this.valueField; 
     } 
     set { 
      this.valueField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
public partial class CatalogItemsItemSizesSize { 

    private string nameField; 

    private string valueField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name { 
     get { 
      return this.nameField; 
     } 
     set { 
      this.nameField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Value { 
     get { 
      return this.valueField; 
     } 
     set { 
      this.valueField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] 
public partial class NewDataSet { 

    private Catalog[] itemsField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Catalog")] 
    public Catalog[] Items { 
     get { 
      return this.itemsField; 
     } 
     set { 
      this.itemsField = value; 
     } 
    } 
} 

反序列化代碼:

System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(Catalog)); 
      TextReader reader = new StreamReader("catalog.xml"); 
      Catalog catalog = (Catalog)xSerializer.Deserialize(reader); 
      foreach (var item in catalog.Items) 
      { 


      } 


    reader.Close(); 

當我通過有存在於目錄一個項目的代碼SETP .items,但它是空的,沒有名字,顏色或大小。

任何想法我需要做什麼?

謝謝

回答

1

XSD並不完美 - 在這種情況下,它肯定會錯誤的!

就我而言,我有這樣的代碼:

public partial class Catalog { 
    private CatalogItemsItem[][] itemsField; 
    private string nameField; 

    /// <remarks/> 
    [XmlArray(Form=XmlSchemaForm.Unqualified)] 
    [XmlArrayItem("Item", typeof(CatalogItemsItem), 
    Form=XmlSchemaForm.Unqualified, IsNullable=false)] 
    public CatalogItemsItem[][] Items { 
     get { 


......  

public partial class CatalogItemsItem { 
    private CatalogItemsItemColorsColor[][] colorsField; 
    private CatalogItemsItemSizesSize[][] sizesField; 

那些「數組的數組」是一個典型的錯誤,你有時會遇到XSD。我甚至無法反序列化您的測試XML!

一旦我改變這只是簡單的一維數組,一切正常。

public partial class Catalog { 
    private CatalogItemsItem[] itemsField; 
    private string nameField; 

    /// <remarks/> 
    [XmlArray(Form=XmlSchemaForm.Unqualified)] 
    [XmlArrayItem("Item", typeof(CatalogItemsItem), 
    Form=XmlSchemaForm.Unqualified, IsNullable=false)] 
    public CatalogItemsItem[] Items { 
     get { 


......  

public partial class CatalogItemsItem { 
    private CatalogItemsItemColorsColor[] colorsField; 
    private CatalogItemsItemSizesSize[] sizesField; 

不,對不起,我不知道任何開關,將允許你告訴XSD表現得像這一切的時候 - 它手冊「後的一代,清理工作「這需要在這裏完成。

1

xsd.exe生成不正確的代碼。您的類不是太難做手工吧:

public class Color 
{ 
    [XmlAttribute] 
    public string Name; 

    [XmlAttribute] 
    public string Value; 
} 

public class Size 
{ 
    [XmlAttribute] 
    public string Name; 

    [XmlAttribute] 
    public string Value; 
} 

public class Item 
{ 
    public Color[] Colors; 

    public Size[] Sizes; 
} 

public class Catalog 
{ 
    [XmlAttribute] 
    public string Name; 

    public Item[] Items; 
} 
+0

感謝您的回答。你是對的,但我提供的例子只是我想要做的簡化版本。我只是想把基礎知識放在第一位。我只是假設由xsd生成的代碼是正確的。 – 2010-03-24 18:46:25

+0

輪到我發現,沒有xsd的情況下編寫這樣的代碼並不困難:) – Andrey 2010-03-24 19:02:03