0

谁能建议如何创建 ac# 类来反映以下 XML 结构示例?基本上,元素Value中的子元素可以包含IntegerString元素。

<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是 Visual Studio 附带的实用程序......

看看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 回答