问题
I am trying to deserialize an XML answer based on classes created by XSD schema but it always returns null.
The XML file has this format
<?xml version="1.0" encoding="utf-16"?>
<Responses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Response>
<inv_number>1</inv_number>
<StatusCode>Success</StatusCode>
<Uid>271D95D28716B37A330A5A476AE530206828B103</Uid>
<Mark>1000000912965</Mark>
</Response>
<Response>
<inv_number>2</inv_number>
<StatusCode>ValidationError</StatusCode>
<Errors>
<Error>
<Message>Author AFM is not the same with User AFM</Message>
<Code>-1</Code>
</Error>
</Errors>
</Response>
</Responses>
My class file based on XSD schema (generated with xsd.exe) is:
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.6.1055.0.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[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 ResponseDoc {
private ResponseType[] responseField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("response")]
public ResponseType[] response {
get {
return this.responseField;
}
set {
this.responseField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ResponseType {
private int entitylineNumberField;
private string statusCodeField;
private object[] itemsField;
/// <remarks/>
public int entitylineNumber {
get {
return this.entitylineNumberField;
}
set {
this.entitylineNumberField = value;
}
}
/// <remarks/>
public string statusCode {
get {
return this.statusCodeField;
}
set {
this.statusCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("entityMark", typeof(long))]
[System.Xml.Serialization.XmlElementAttribute("entityUid", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("errors", typeof(ResponseTypeErrors))]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class ResponseTypeErrors {
private ErrorType[] errorField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("error")]
public ErrorType[] error {
get {
return this.errorField;
}
set {
this.errorField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ErrorType {
private string messageField;
private int codeField;
/// <remarks/>
public string message {
get {
return this.messageField;
}
set {
this.messageField = value;
}
}
/// <remarks/>
public int code {
get {
return this.codeField;
}
set {
this.codeField = value;
}
}
}
My code of deserialize the XML answer is
XmlSerializer serializer = new XmlSerializer(typeof(ResponseType),new XmlRootAttribute("Responses"));
ResponseType resultingResponse = (ResponseType)serializer.Deserialize(new StringReader(result));
If I don't put the
new XmlRootAttribute("Responses")
it gives me an exception
system.invalidOperaionException <Responses xmlns=''> was not expected.
If I use it it just gives me empty results. I also tried deleting the
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
as suggested here but nothing changed.
回答1:
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
StreamReader sReader = new StreamReader(FILENAME);
//read past the unicode in first line
sReader.ReadLine();
XmlReader reader = XmlReader.Create(sReader);
XmlSerializer serializer = new XmlSerializer(typeof(ResponseDoc));
ResponseDoc responseDoc = (ResponseDoc)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "Responses", Namespace = "")]
public partial class ResponseDoc
{
private ResponseType[] responseField;
/// <remarks/>
[System.Xml.Serialization.XmlElement(ElementName = "Response", Namespace = "")]
public ResponseType[] response
{
get
{
return this.responseField;
}
set
{
this.responseField = value;
}
}
}
[XmlRoot(ElementName = "Response", Namespace = "")]
public partial class ResponseType
{
private int entitylineNumberField;
private string statusCodeField;
private string uid;
private long entityMark;
private ResponseTypeErrors[] responseTypeErrors;
/// <remarks/>
[XmlElement(ElementName = "inv_number", Namespace = "")]
public string entitylineNumber
{
get
{
return this.entitylineNumberField.ToString();
}
set
{
this.entitylineNumberField = int.Parse(value);
}
}
/// <remarks/>
[XmlElement(ElementName = "StatusCode", Namespace = "")]
public string statusCode
{
get
{
return this.statusCodeField;
}
set
{
this.statusCodeField = value;
}
}
[XmlElement(ElementName = "Uid", Namespace = "")]
public string Uid
{
get
{
return this.uid;
}
set
{
this.uid = value;
}
}
[XmlElement(ElementName = "entityMark", Namespace = "")]
public string EntityMark
{
get
{
return this.entityMark.ToString();
}
set
{
this.entityMark = long.Parse(value);
}
}
[XmlElement(ElementName = "Errors", Namespace = "")]
public ResponseTypeErrors[] Errors
{
get
{
return this.responseTypeErrors;
}
set
{
this.responseTypeErrors = value;
}
}
}
[XmlRoot(ElementName = "Errors", Namespace = "")]
public partial class ResponseTypeErrors
{
private ErrorType[] errorField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Error")]
public ErrorType[] error
{
get
{
return this.errorField;
}
set
{
this.errorField = value;
}
}
}
[XmlRoot(ElementName = "Error", Namespace = "")]
public partial class ErrorType
{
private string messageField;
private int codeField;
/// <remarks/>
[XmlElement(ElementName = "Message", Namespace = "")]
public string message
{
get
{
return this.messageField;
}
set
{
this.messageField = value;
}
}
/// <remarks/>
[XmlElement(ElementName = "Code", Namespace = "")]
public int code
{
get
{
return this.codeField;
}
set
{
this.codeField = value;
}
}
}
}
来源:https://stackoverflow.com/questions/57748327/deserialize-an-xml-string-based-on-class-file-generated-with-xsd