C# xml多数组元素序列化

C# xml多数组元素序列化,c#,.net,xml,serialization,C#,.net,Xml,Serialization,各位。 我需要用内部元素序列化对象,这些元素可以出现多次,并且每个元素都是数组。 比如说 <Entity_Spatial Ent_Sys="ID0"> <Spatial_Element> <Spelement_Unit Type_Unit="Точка"> <NewOrdinate X="1234567890123456789" Y="1234567890123456789" Num_Geopo

各位。 我需要用内部元素序列化对象,这些元素可以出现多次,并且每个元素都是数组。 比如说

<Entity_Spatial Ent_Sys="ID0">
        <Spatial_Element>
          <Spelement_Unit Type_Unit="Точка">
            <NewOrdinate X="1234567890123456789" Y="1234567890123456789" Num_Geopoint="1073741824"/>
          </Spelement_Unit>
          <Spelement_Unit Type_Unit="Точка">
            <NewOrdinate X="1234567890123456789" Y="1234567890123456789" Num_Geopoint="1073741824"/>
          </Spelement_Unit>
          <Spelement_Unit Type_Unit="Точка">
            <NewOrdinate X="1234567890123456789" Y="1234567890123456789" Num_Geopoint="1073741824"/>
          </Spelement_Unit>
          <Spelement_Unit Type_Unit="Точка">
            <NewOrdinate X="1234567890123456789" Y="1234567890123456789" Num_Geopoint="1073741824"/>
          </Spelement_Unit>
        </Spatial_Element>
        <Spatial_Element>
          <Spelement_Unit Type_Unit="Точка">
            <NewOrdinate X="1234567890123456789" Y="1234567890123456789" Num_Geopoint="1073741824"/>
          </Spelement_Unit>
          <Spelement_Unit Type_Unit="Точка">
            <NewOrdinate X="1234567890123456789" Y="1234567890123456789" Num_Geopoint="1073741824"/>
          </Spelement_Unit>
          <Spelement_Unit Type_Unit="Точка">
            <NewOrdinate X="1234567890123456789" Y="1234567890123456789" Num_Geopoint="1073741824"/>
          </Spelement_Unit>
          <Spelement_Unit Type_Unit="Точка">
            <NewOrdinate X="1234567890123456789" Y="1234567890123456789" Num_Geopoint="1073741824"/>
          </Spelement_Unit>
        </Spatial_Element>
      </Entity_Spatial>

此类的XSD架构:

<xs:element name="Entity_Spatial">
    <xs:annotation>
        <xs:documentation>Описание местоположения границ</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Spatial_Element" type="tSPATIAL_ELEMENT_SHORT" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>Элемент контура</xs:documentation>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="Ent_Sys" type="xs:IDREF">
            <xs:annotation>
                <xs:documentation>Ссылка на систему координат</xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
</xs:element>
<xs:complexType name="tSPATIAL_ELEMENT_SHORT">
    <xs:annotation>
        <xs:documentation>Элемент контура</xs:documentation>
    </xs:annotation>
    <xs:sequence maxOccurs="unbounded">
        <xs:element name="Spelement_Unit" type="tSPELEMENT_UNIT_SHORT" maxOccurs="unbounded">
            <xs:annotation>
                <xs:documentation>Часть элемента (точка)</xs:documentation>
            </xs:annotation>
        </xs:element>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="tSPELEMENT_UNIT_SHORT">
    <xs:annotation>
        <xs:documentation>Части элементов контуров (точка)</xs:documentation>
    </xs:annotation>
    <xs:sequence>
        <xs:element name="NewOrdinate" type="tOrdinate_short" minOccurs="1">
            <xs:annotation>
                <xs:documentation>Новая точка</xs:documentation>
            </xs:annotation>
        </xs:element>
    </xs:sequence>
    <xs:attribute name="Type_Unit" use="required" fixed="Точка">
        <xs:annotation>
            <xs:documentation>"Элементарный" тип для части элемента</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
            <xs:restriction base="Type_Unit"/>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>
<xs:complexType name="tOrdinate_short">
    <xs:annotation>
        <xs:documentation>Координата</xs:documentation>
    </xs:annotation>
    <xs:attribute name="X" use="required">
        <xs:annotation>
            <xs:documentation>Координата X</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:totalDigits value="35"/>
                <xs:fractionDigits value="0"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="Y" use="required">
        <xs:annotation>
            <xs:documentation>Координата Y</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:totalDigits value="35"/>
                <xs:fractionDigits value="0"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="Num_Geopoint">
        <xs:annotation>
            <xs:documentation>Номер межевой точки</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
            <xs:restriction base="xs:positiveInteger">
                <xs:totalDigits value="22"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>

Описание местоположения границ
Элемент контура
Ссылка на систему координат
Элемент контура
Часть элемента (точка)
Части элементов контуров (точка)
Новая точка
"Элементарный" тип для части элемента
Координата
ааааX
ааааY
Номер межевой точки
由visual studio的xsd.exe生成的类实体_Spatial:

/// <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 Entity_Spatial : object, System.ComponentModel.INotifyPropertyChanged
{

    private tSPELEMENT_UNIT_SHORT[][]spatial_ElementField;

    private string ent_SysField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("Spelement_Unit", typeof(tSPELEMENT_UNIT_SHORT), IsNullable = false)]
    public tSPELEMENT_UNIT_SHORT[][] Spatial_Element
    {
        get
        {
            return this.spatial_ElementField;
        }
        set
        {
            this.spatial_ElementField = value;
            this.RaisePropertyChanged("Spatial_Element");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "IDREF")]
    public string Ent_Sys
    {
        get
        {
            return this.ent_SysField;
        }
        set
        {
            this.ent_SysField = value;
            this.RaisePropertyChanged("Ent_Sys");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null))
        {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}
//
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类实体:对象,System.ComponentModel.INotifyPropertyChanged
{
专用tSPELEMENT_UNIT_SHORT[]空间元素字段;
私有字符串ent_系统字段;
/// 
[System.Xml.Serialization.XmlArrayItemAttribute(“Spelement_Unit”,typeof(tSPELEMENT_Unit_SHORT),IsNullable=false]
公共tSPELEMENT\u单元\u短[]空间元素
{
得到
{
返回此.spatial_元素字段;
}
设置
{
this.spatial_element字段=值;
此.RaisePropertyChanged(“空间元素”);
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute(DataType=“IDREF”)]
公共字符串输入系统
{
得到
{
返回此.ent_系统字段;
}
设置
{
this.ent_SysField=值;
本.RaisePropertyChanged(“Ent_系统”);
}
}
公共事件System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
受保护的void RaisePropertyChanged(字符串propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged=this.propertyChanged;
如果((propertyChanged!=null))
{
propertyChanged(这是新的System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
尝试序列化对象时出现问题。例外情况


“无法生成临时类(结果=1)


错误CS0030:无法将类型“tSPELEMENT\u UNIT\u SHORT[]”转换为“tSPELEMENT\u UNIT\u SHORT”


错误CS0029:无法将类型“tSPELEMENT\u UNIT\u SHORT”转换为抛出的“tSPELEMENT\u UNIT\u SHORT[]”


我需要解决这个问题,但无法更改xml模式。我该怎么办?

试试这个

public class Spatial_Element
{
    [XmlElement("Spelement_Unit")]
    public List<Spelement_Unit> units;

    public Spatial_Element()
    {
        units = new List<Spelement_Unit>();
    }
}
public class Spelement_Unit
{
    [XmlAttribute("Type_Unit")]
    public string type_unit;
    [XmlElement("NewOrdinate")]
    public NewOrdinate newOrdinate;
}
public class NewOrdinate
{
    [XmlAttribute("X")]
    public string X;
    [XmlAttribute("Y")]
    public string Y;
    [XmlAttribute("Num_Geopoint")]
    public string Num_Geopoint;
}
public class Entity_Spatial
{
    [XmlAttribute("Ent_Sys")]
    public string Ent_Sys;

    [XmlElement("Spatial_Element")]
    public List<Spatial_Element> Items;

    public Entity_Spatial()
    {
        Items = new List<Spatial_Element>();
    }
}
替换它

[System.Xml.Serialization.XmlArrayItemAttribute("Spelement_Unit", typeof(tSPELEMENT_UNIT_SHORT), IsNullable = false)]
在那

[System.Xml.Serialization.XmlArrayItemAttribute("Spelement_Unit", typeof(tSPELEMENT_UNIT_SHORT[]), IsNullable = false)]

谢谢你,@Adrian,但是Alexander的方法更容易。不幸的是,这不是我真正需要的。结果是。。
[System.Xml.Serialization.XmlArrayItemAttribute("Spelement_Unit", typeof(tSPELEMENT_UNIT_SHORT[]), IsNullable = false)]