在我的xml上运行XSD.exe以生成C#类。一切都很好,除了这个属性
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("ATTRIBUTES", typeof(DOCUMENTDocumentATTRIBUTES), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public DocumentATTRIBUTES[][] Document {
get {
return this.documentField;
}
set {
this.documentField = value;
}
}
我想尝试使用CollectionBase,这是我的尝试
public DocumentATTRIBUTESCollection Document {
get {
return this.documentField;
}
set {
this.documentField = value;
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DocumentATTRIBUTES
{
private string _author;
private string _maxVersions;
private string _summary;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string author
{
get
{ return _author; }
set { _author = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string max_versions
{
get { return _maxVersions; }
set { _maxVersions = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string summary
{
get { return _summary; }
set { _summary = value; }
}
}
public class DocumentAttributeCollection : System.Collections.CollectionBase
{
public DocumentAttributeCollection() : base() { }
public DocumentATTRIBUTES this[int index]
{
get
{
return (DocumentATTRIBUTES)this.InnerList[index];
}
}
public void Insert(int index, DocumentATTRIBUTES value)
{
this.InnerList.Insert(index, value);
}
public int Add(DocumentATTRIBUTES value)
{
return (this.InnerList.Add(value));
}
}
但是当我尝试使用
序列化我的对象时XmlSerializer serializer = new XmlSerializer(typeof(DocumentMetaData));
我收到错误:
{"Unable to generate a temporary class (result=1).\r\nerror CS0030:
Cannot convert type 'DocumentATTRIBUTES' to 'DocumentAttributeCollection'\r\nerror CS1502: The best overloaded method match for 'DocumentAttributeCollection.Add(DocumentATTRIBUTES)' has some invalid arguments\r\nerror CS1503: Argument '1': cannot convert from 'DocumentAttributeCollection' to 'DocumentATTRIBUTES'\r\n"}
与此属性相关的XSD是
<xs:complexType>
<xs:sequence>
<xs:element name="ATTRIBUTES" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="author" type="xs:string" minOccurs="0" />
<xs:element name="max_versions" type="xs:string" minOccurs="0" />
<xs:element name="summary" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
更新: 我改为:
[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("DocumentAttributes", typeof(DocumentAttributes), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public DocumentAttributeCollection Document
{
get { return _documentField; }
set { _documentField = value; }
}
我遇到了麻烦的错误,但我的序列化对象没有与'ATTRIBUTES'元素有关的数据。
答案 0 :(得分:1)
解决: 我在我的代码中有这个
[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("DocumentAttributes", typeof(DocumentAttributes), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public DocumentAttributeCollection Document
{
get { return _documentField; }
set { _documentField = value; }
}
我错误地将属性状态'DocumentAttributes'而不是'ATTRIBUTES'
将属性更改为
[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("ATTRIBUTES", typeof(DocumentAttributes), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public DocumentAttributeCollection Document
{
get { return _documentField; }
set { _documentField = value; }
}