0

次のXML構造の例を反映するac#クラスを作成する方法を誰かが提案できますか?基本的に、要素Value内のサブ要素には、整数要素または文字列要素のいずれかを含めることができます。

<Preferences>
   <Preference>
      <Tag>CY3A</Tag>
      <Precedence>XML</Precedence>
      <Value>
         <Length>4</Length>
         <Integer>0</Integer>
      </Value>
   </Preference>    
   <Preference>
      <Tag>CYRV</Tag>
      <Precedence>XML</Precedence>
      <Value>
         <Length>16</Length>
         <String>0000 00 @00000</String>
      </Value>
   </Preference>
</Preferences>

次のようにクラスを定義しようとしましたが、探しているものになりませんでした。不要なd要素を生成します。

public class ProgrammingConfiguration
{
   public int Version { get; set; }
   public preferences Preferences { get; set; }
}

public class preferences
{
   public preference Preference { get; set; }
}

public class preference
{
   public string Tag { get; set; }
   public string Precedence { get; set; }                                     
   public value Value { get; set; }
}

public class value
{
   [XmlElement]
   public int Length;

   [XmlElement(Type = typeof(Integer)),
    XmlElement(Type = typeof(HexBinary))]
   public object datafield;
}

public class Integer
{          
   public string d;            
}  

public class String
{            
   public string d;
}        
4

1 に答える 1

0

私は通常、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.
}
于 2012-09-24T09:27:18.247 に答える