关于XMLSerializer序列化必选字段的问题
这是我先定义的xsd中的一部分,这四个字段我都想让它门成为必选字段.<xs:element name="TestString" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="TestInteger" type="xs:integer" maxOccurs="1" minOccurs="1" />
<xs:element name="TestDate" type="xs:date" maxOccurs="1" minOccurs="1" />
<xs:element name="TestBool" type="xs:boolean" maxOccurs="1" minOccurs="1" />
然后我用工具生成下面的C#类,采用是的XMLSerializer来序列化它
/// <remarks/>
public string TestString {
get {
return this.testStringField;
}
set {
this.testStringField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
public string TestInteger {
get {
return this.testIntegerField;
}
set {
this.testIntegerField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="date")]
public System.DateTime TestDate {
get {
return this.testDateField;
}
set {
this.testDateField = value;
}
}
/// <remarks/>
public bool TestBool {
get {
return this.testBoolField;
}
set {
this.testBoolField = value;
}
}
通过Host发布服务,发现里面的结构发生了变化
<xs:element minOccurs="0" maxOccurs="1" name="TestString" type="xs:string"/>
<xs:element minOccurs="0" maxOccurs="1" name="TestInteger" type="xs:integer"/>
<xs:element minOccurs="1" maxOccurs="1" name="TestDate" type="xs:date"/>
<xs:element minOccurs="1" maxOccurs="1" name="TestBool" type="xs:boolean"/>
定义成string和integer类型的 minOccurs属性从当初定义的1变成了0
请问怎么解决这个问题.
--------------------编程问答-------------------- http://msdn2.microsoft.com/en-us/library/d3hx2s7e(VS.80).aspx --------------------编程问答-------------------- Example
Input XML schema <choice> element found within a complex type definition:
Copy Code
<xsd:choice maxOccurs="unbounded">
<xsd:element name="stringA" type="xsd:string"/>
<xsd:element name="stringB" type="xsd:string"/>
</xsd:choice>
Relevant passages from the C# class generated from the preceding XML Schema document, plus the enumeration representing the element choices, (assuming a target namespace of http://example.org/):
Copy Code
[System.Xml.Serialization.XmlElementAttribute("stringA", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("stringB", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public string[] Items;
[System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemsChoiceType[] ItemsElementName;
...
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/", IncludeInSchema=false)]
public enum ItemsChoiceType {
stringA,
stringB,
}
The complex type generated from a class compiled from the preceding C# source code is effectively equivalent to the original complex type.
<sequence>
Example
Input XML Schema document containing a sequence with maxOccurs greater than1:
Copy Code
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.org/" targetNamespace="http://example.org/" elementFormDefault="qualified">
<xsd:element name="ComplexInstance">
<xsd:complexType>
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="Field1" type="xsd:token"/>
<xsd:element name="Field2" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
C# class generated from the preceding XML Schema document without any command line options:
Copy Code
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://example.org/", IsNullable=false)]
public class ComplexInstance {
[System.Xml.Serialization.XmlElementAttribute("Field1", DataType="token")]
public string[] Field1;
[System.Xml.Serialization.XmlElementAttribute("Field2")]
public int[] Field2;
}
XML Schema complex type generated from an assembly compiled from the preceding C# source:
Copy Code
<xs:complexType name="ComplexInstance">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Field1" type="xs:token" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="Field2" type="xs:int" />
</xs:sequence>
</xs:complexType>
As you can see, the resulting schema is not equivalent to the original schema. To properly import a schema having sequences with maxOccurs greater than1, use the /order command line option, resulting in the following:
[System.Xml.Serialization.XmlTypeAttribute
(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute
(Namespace="http://example.org/", IsNullable=false)]
public partial class ComplexInstance
{
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute
("Field1", typeof(string), DataType="token", Order=0)]
[System.Xml.Serialization.XmlElementAttribute("Field2",
typeof(int), Order=0)]
public object[] Items;
}
--------------------编程问答-------------------- 这个不是很会,帮你顶 --------------------编程问答-------------------- ting --------------------编程问答-------------------- http://kjellsj.blogspot.com/2006/12/how-to-expose-wcf-service-also-as-asmx.html
"You will get minOccurs=0 in the generated WSDL for string fields, as string is a reference type. This is because the XmlSerializer by default makes reference types optional, while value types by default are mandatory."大体意思是,string是引用类型,XmlSerializer缺省情况下将引用类型置为可选,将值类型置为强制的(mandatory)。
http://msdn2.microsoft.com/en-us/library/zds0b35c.aspx这里更详细,“For the <element> element, Xsd.exe also ignores the minOccurs attribute if it is applied to a schema data type that converts to a .NET Framework reference type.
Only when all the following conditions are true does Xsd.exe utilize the value of the minOccurs attribute:
The <element> element is involved.
The maxOccurs attribute dictates a single instance.
The data type converts to a value type.
.....
” --------------------编程问答-------------------- 关注 接分 --------------------编程问答-------------------- 以后需再关注,现在先帮你顶一下 --------------------编程问答-------------------- 什么呀
补充:.NET技术 , Web Services