【发布时间】:2012-02-21 11:01:10
【问题描述】:
我正在尝试反序列化这个 xml:
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
yahoo:count="10" yahoo:created="2012-02-20T19:07:02Z" yahoo:lang="en-US">
<results>
<a href="/dir/index?sid=396545299">Books & Authors</a>
<a href="/dir/index?sid=396545374">Dancing</a>
<a href="/dir/index?sid=396546034">Genealogy</a>
<a href="/dir/index?sid=396545298">History</a>
<a href="/dir/index?sid=396545310">Other - Arts & Humanities</a>
<a href="/dir/index?sid=396545300">Performing Arts</a>
<a href="/dir/index?sid=396545231">Philosophy</a>
<a href="/dir/index?sid=2115500137">Poetry</a>
<a href="/dir/index?sid=396546419">Theater & Acting</a>
<a href="/dir/index?sid=396545309">Visual Arts</a>
</results>
</query>
这是yahoo api在查询类别和子类别时返回的xml。
这段代码应该反序列化它:
string query_string = "some_url";
HttpWebRequest request = HttpWebRequest.Create
(query_string) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
System.Xml.Serialization.XmlSerializer serializer = new
System.Xml.Serialization.XmlSerializer(typeof(queryA));
queryA Q = (queryA)serializer.Deserialize(reader); // THIS IS WHERE THE EXCEPTION IS THROWN
reader.Close();
}
我得到一个异常提示:XML 文档 (2, 2) 中存在错误。
异常详情:
"<query xmlns=''> was not expected."
课程如下:
namespace Yahoo_answers_tool {
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[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 queryA {
private queryResultsA[] resultsField;
private string countField;
private string createdField;
private string langField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("a", typeof(queryResultsA), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public queryResultsA[] results {
get {
return this.resultsField;
}
set {
this.resultsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace="http://www.yahooapis.com/v1/base.rng")]
public string count {
get {
return this.countField;
}
set {
this.countField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace="http://www.yahooapis.com/v1/base.rng")]
public string created {
get {
return this.createdField;
}
set {
this.createdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified, Namespace="http://www.yahooapis.com/v1/base.rng")]
public string lang {
get {
return this.langField;
}
set {
this.langField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class queryResultsA {
private string hrefField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string href {
get {
return this.hrefField;
}
set {
this.hrefField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[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 NewDataSetA {
private queryA[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("query")]
public queryA[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
}
有什么想法吗?
【问题讨论】:
-
我怀疑它不喜欢命名空间。
-
哪个工具生成了该代码?我使用 Xsd2Code,从来没有遇到过这样的问题。你生成的代码对我来说看起来很奇怪(类的命名)
-
您尝试过这里描述的内容吗? [将 Xml 反序列化为对象时出错 - xmlns='' 不是预期的][1] [1]: stackoverflow.com/questions/4884383/…
-
你为什么要反序列化它,为什么不简单地使用 Linq To Xml 或其他 XML perser 来获取你想要的数据?
-
@BlueM 我正在使用 Visual Studio 自带的 XSD.exe
标签: c# deserialization xmlserializer