我有这个 XML 文件:
<?xml version="1.0" encoding="us-ascii"?>
<body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<header><user>BOBBY</user></header>
<in>
<customer>0123456789</customer>
</in>
<out>
<cmd>
<productid></productid>
<price></price>
<date></date>
<state></state>
<type></type>
</cmd>
<cmd>
<productid></productid>
<price></price>
<date></date>
<state></state>
<type></type>
</cmd>
</out>
<state>
<code></code>
<desc></desc>
</state>
</body>
使用该文件,我生成了这个 CSS 类:
//------------------------------------------------------------------------------
// <auto-generated>
// Ce code a été généré par un outil.
// Version du runtime :2.0.50727.5456
//
// Les modifications apportées à ce fichier peuvent provoquer un comportement incorrect et seront perdues si
// le code est régénéré.
// </auto-generated>
//------------------------------------------------------------------------------
namespace DAL.XML.PDD.Test
{
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[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 body
{
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("enter", typeof(bodyEnter), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public bodyEnter enter = new bodyEnter();
[System.Xml.Serialization.XmlElementAttribute("header", typeof(bodyHeader), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public bodyHeader header = new bodyHeader();
[System.Xml.Serialization.XmlElementAttribute("result", typeof(bodyResult), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public bodyResult result = new bodyResult();
[System.Xml.Serialization.XmlElementAttribute("state", typeof(bodyState), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public bodyState state = new bodyState();
public object[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class bodyEnter
{
private string customerField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string customer
{
get
{
return this.customerField;
}
set
{
this.customerField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class bodyHeader
{
private string userField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string user
{
get
{
return this.userField;
}
set
{
this.userField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class bodyResult
{
private bodyResultCmd[] cmdField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("cmd", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public bodyResultCmd[] cmd
{
get
{
return this.cmdField;
}
set
{
this.cmdField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class bodyResultCmd
{
private string productidField;
private string priceField;
private string dateField;
private string stateField;
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string productid
{
get
{
return this.productidField;
}
set
{
this.productidField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string price
{
get
{
return this.priceField;
}
set
{
this.priceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string date
{
get
{
return this.dateField;
}
set
{
this.dateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string state
{
get
{
return this.stateField;
}
set
{
this.stateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class bodyState
{
private string codeField;
private string descField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string code
{
get
{
return this.codeField;
}
set
{
this.codeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string desc
{
get
{
return this.descField;
}
set
{
this.descField = value;
}
}
}
}
这很好用,但是当我对其进行序列化时,空属性和元素或不存在:
<?xml version="1.0" encoding="utf-8"?>
<body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<enter>
<customer>0123456789</customer>
</enter>
<header>
<user>Bobby</user>
</header>
<result />
<state />
</body>
我在我的 Cs 类中添加了这个代码:
[XmlElement(IsNullable=true)]
[System.Xml.Serialization.XmlElementAttribute("enter", typeof(bodyEnter), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public bodyEnter enter = new bodyEnter();
但我有这个错误:
**InvalidOperationException**
谢谢。