2010-03-24 1 views
0

비 직렬화 된 XML 객체가 있습니다. 모든 항목을 표시하려면 oject를 반복해야합니다.이 경우 하나만 있고, 각 항목의 이름, 색상 및 크기가 표시됩니다.deserialized 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(); 

I 카탈로그에서 하나 개의 항목이 존재할 수있는 코드를 통해 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을 deserialize 할 수 없었다!

일단 이것을 단순한 1 차원 배열로 변경하면 모든 것이 잘 작동합니다.

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에서 잘못된 코드가 생성되었습니다. 당신이 특정 방식에 부착하지 않는 당신이 할 필요가 XML을 구문 분석하는 경우

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

감사합니다. 당신 말이 맞습니다. 그러나 제가 제공 한 예는 제가하고 싶은 것을 단순화 한 것입니다. 나는 그저 바른 fisrt을 얻고 싶었다. xsd에 의해 생성 된 코드가 정확하다고 가정했습니다. –

+0

내 차례대로 그것은 xsd없이 그런 코드를 작성하는 것이 어렵지 않다는 것을 보여주었습니다 :) – Andrey

1

, 나는 높은 LINQ - 투 - XML을가는 것이 좋습니다 : 귀하의 클래스는 손으로 그것을 너무 단단하지 않다 경로, 그것은 내 인생을 훨씬 쉽게 만들었습니다.

기본 사항 : 대한 답변을 http://msdn.microsoft.com/en-us/library/bb387087.aspx