Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML枚举反序列化_C#_Xml_Enums_Xsd_Deserialization - Fatal编程技术网

C# XML枚举反序列化

C# XML枚举反序列化,c#,xml,enums,xsd,deserialization,C#,Xml,Enums,Xsd,Deserialization,我有一个从以下XSD生成的C#枚举 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="mails" type="mailsType" /> <xsd:complexType name="mailsType"> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:e

我有一个从以下XSD生成的C#枚举

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <xsd:element name="mails" type="mailsType" />

 <xsd:complexType name="mailsType">
  <xsd:sequence minOccurs="0" maxOccurs="unbounded">
   <xsd:element name="mail" type="mailType" />
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="mailType">
  <xsd:sequence>
   <xsd:element name="envelope" type="envelopeType" />
   <xsd:element name="body" type="bodyType" />
   <xsd:element name="attachment" type="attachmentType"
        minOccurs="0" maxOccurs="unbounded" />
  </xsd:sequence>
  <xsd:attribute use="required" name="id" type="xsd:integer" />
 </xsd:complexType>

 <xsd:element name="header">
  <xsd:complexType>
   <xsd:simpleContent>
    <xsd:extension base="xsd:string">
     <xsd:attribute ref="name" use="required" />
    </xsd:extension>
   </xsd:simpleContent>
  </xsd:complexType>
 </xsd:element>

 <xsd:element name="Date" type="xsd:dateTime" />

 <xsd:complexType name="envelopeType">
  <xsd:sequence>
   <xsd:element name="From" type="xsd:string" />
   <xsd:element name="To" type="xsd:string" />
   <xsd:element ref="Date" />
   <xsd:element name="Subject" type="xsd:string" />
   <xsd:element ref="header" minOccurs="0" maxOccurs="unbounded" />
  </xsd:sequence>
  <xsd:attribute name="From" type="xsd:string" use="required" />
 </xsd:complexType>

 <xsd:simpleType name="bodyType">
  <xsd:restriction base="xsd:string" />
 </xsd:simpleType>

 <xsd:complexType name="attachmentType">
  <xsd:group ref="attachmentContent" />
  <xsd:attribute ref="name" use="required" />
 </xsd:complexType>

 <xsd:group name="attachmentContent">
  <xsd:sequence>
   <xsd:element name="mimetype">
    <xsd:complexType>
     <xsd:attributeGroup ref="mimeTypeAttributes" />
    </xsd:complexType>
   </xsd:element>
   <xsd:element name="content" type="xsd:string" minOccurs="0" />
  </xsd:sequence>
 </xsd:group>

 <xsd:attribute name="name" type="xsd:string" />

 <xsd:attributeGroup name="mimeTypeAttributes">
  <xsd:attribute name="type" type="mimeTopLevelType" use="required" />
  <xsd:attribute name="subtype" type="xsd:string" use="required" />
 </xsd:attributeGroup>

 <xsd:simpleType name="mimeTopLevelType">
  <xsd:restriction base="xsd:string">
   <xsd:enumeration value="text" />
   <xsd:enumeration value="multipart" />
   <xsd:enumeration value="application" />
   <xsd:enumeration value="message" />
   <xsd:enumeration value="image" />
   <xsd:enumeration value="audio" />
   <xsd:enumeration value="video" />
  </xsd:restriction>
 </xsd:simpleType>

</xsd:schema>

我有一个C#代码:

//
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
公共枚举mimeTopLevelType{
/// 
文本,
/// 
多部分,
/// 
应用
/// 
消息
/// 
形象,,
/// 
音频
/// 
视频
}
我有这个XML(略图):


...
###被泥沼移走###
当我尝试将此XML反序列化为C#对象时,出现以下错误:

XML文档(14,17)中有一个错误

显然,问题在于XML有一个属性为“application”的元素,无法将其转换为枚举类型


有人能为这个问题提出解决方案吗?

难怪——你有

type="application " 
而不是

type="application"

注意属性末尾的空格。

你知道吗?我现在检查了它,但我正在使用.net内置的xsd命令从xsd创建c#类。如果我使用Xsd2Code有什么不同吗?您应该继续这样做,然后使用VS中的插件生成类,它还将生成用于对对象进行反序列化/序列化的方法。除此之外,您还可以控制类的生成。您能显示有问题的第14行吗?xsd2Code在生成的代码中使用了.net的内置反序列化程序,因此它不能解决我的问题。我不想更改任何属性或元素值。@SoyhanBeyazıt-那么您运气不好-属性为的xml文档根据您声明的xsd,设置为type=“application”无效。如果您想解析这种糟糕的文档,您必须手动执行,或者重新声明mimeTopLevelType为string(无限制)。
type="application " 
type="application"