비 직렬화 된 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.하지만 비어 있거나, 이름, 색상 또는 크기가 없습니다.
내가해야 할 아이디어가 있습니까?
감사
감사합니다. 당신 말이 맞습니다. 그러나 제가 제공 한 예는 제가하고 싶은 것을 단순화 한 것입니다. 나는 그저 바른 fisrt을 얻고 싶었다. xsd에 의해 생성 된 코드가 정확하다고 가정했습니다. –
내 차례대로 그것은 xsd없이 그런 코드를 작성하는 것이 어렵지 않다는 것을 보여주었습니다 :) – Andrey