xml多数组元素序列化
本文关键字:序列化 数组元素 xml | 更新日期: 2023-09-27 18:02:29
每个人。我需要用内部元素序列化对象,这些元素可以出现多次,并且每个元素都是数组。例如
<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[]"转换为"tSPEELEMENT_UNIT_SHORT"。
错误CS0029:无法将类型"tSPELEMENT_UNIT_SHORT"转换为"tSPEELEMENT_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)]