本文介绍了基于XSD生成的类文件反序列化XML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图基于XSD架构创建的类反序列化XML答案,但它始终返回null。
I am trying to deserialize an XML answer based on classes created by XSD schema but it always returns null.
XML文件具有这种格式
The XML file has this format
<?xml version="1.0" encoding="utf-16"?> <Responses xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:xsd="www.w3/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>基于XSD架构(使用xsd.exe生成)的类文件是:
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; } } }我反序列化XML的代码答案是
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));如果我不输入
新的XmlRootAttribute( Responses)
new XmlRootAttribute("Responses")
它给了我一个例外
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)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
根据建议此处
推荐答案尝试以下操作:
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; } } } }更多推荐
基于XSD生成的类文件反序列化XML字符串
发布评论