簡體   English   中英

xml多數組元素序列化

[英]xml multiple array element serialization

大家。 我需要使用內部元素對對象進行序列化,內部元素可以多次出現,並且每個元素都是數組。 例如

<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>

由Visual Studio的xsd.exe生成的Entity_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));
        }
    }
}

當我嘗試序列化對象時出現問題。 例外


“無法生成臨時類(結果= 1)。


錯誤CS0030:無法將類型'tSPELEMENT_UNIT_SHORT []'轉換為'tSPELEMENT_UNIT_SHORT'。


錯誤CS0029:不能將類型'tSPELEMENT_UNIT_SHORT'轉換為'tSPELEMENT_UNIT_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>();
    }
}

測試

Entity_Spatial es = new Entity_Spatial();
es.Ent_Sys = "ID0";
Spatial_Element se = new Spatial_Element();
Spelement_Unit unit = new Spelement_Unit();
NewOrdinate ordinate = new NewOrdinate();
unit.type_unit = "blabla";
ordinate.X = "500";
ordinate.Y = "800";
ordinate.Num_Geopoint ="1233";
unit.newOrdinate = ordinate;
se.units.Add(unit);
es.Items.Add(se);

XmlSerializer xs = new XmlSerializer(typeof(Entity_Spatial));
xs.Serialize(XmlWriter.Create(@"C:\asd.xml"), es);

Entity_Spatial es2 = (Entity_Spatial)xs.Deserialize(XmlReader.Create(@"C:\test.xml"));

代替它

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

在那個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM