私は通常、xsdを作成してから、xsd.exeツールを使用してxsdから.csクラスファイルを作成します。
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.269
//
// 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=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[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 Preferences {
private PreferencesPreference[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Preference", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PreferencesPreference[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class PreferencesPreference {
private string tagField;
private string precedenceField;
private PreferencesPreferenceValue[] valueField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Tag {
get {
return this.tagField;
}
set {
this.tagField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Precedence {
get {
return this.precedenceField;
}
set {
this.precedenceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Value", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PreferencesPreferenceValue[] Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class PreferencesPreferenceValue {
private string lengthField;
private string stringField;
private string integerField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Length {
get {
return this.lengthField;
}
set {
this.lengthField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string String {
get {
return this.stringField;
}
set {
this.stringField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Integer {
get {
return this.integerField;
}
set {
this.integerField = value;
}
}
}
xsd.exeは、VisualStudioに付属しているユーティリティです。
http://quickstart.developerfusion.co.uk/quickstart/howto/doc/xmlserialization/XSDFromCls.aspxをご覧ください
しかし、XMLからもそれを行うことができます
c:\temp>xsd test.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'c:\temp\test.xsd'.
c:\temp>xsd test.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'c:\temp\test.cs'.
文字列と整数の問題を解決するには、xsdにxs:choiceフィールドを追加します。これにより、「アイテム」が生成され、必要に応じてIntまたはStringとしてキャストできます。http://msdn.microsoft.com/en-us/library/exchange/bb426064(v=exchg.140).aspxをご覧ください
<xs:complexType name="FindItemParentType">
<xs:choice>
<xs:element name="Items" type="t:ArrayOfRealItemsType"/>
<xs:element name="Groups" type="t:ArrayOfGroupedIetmsType"/>
</xs:choice>
<xs:attributeGroup ref="t:FindResponsePagingAttributes"/>
</xs:complexType>
FindItemParentType fipt = new FindItemParentType();
object obj = fipt.Item;
if (obj is ArrayOfGroupedItemsType)
{
ArrayOfGroupedItemsType groupedItems = (obj as ArrayOfGroupedItemsType);
//TODO: Write code to handle grouped items.
}
else if (obj is ArrayOfRealItemsType)
{
ArrayOfRealItemsType items = (obj as ArrayOfRealItemsType);
//TODO: Write code to handle items.
}