如何从 GreatSchool API 响应反序列化
how to deserialize from GreatSchool API Response
我必须从 XML (greatschool API) 反序列化到 Class 域。
URL :
http://www.greatschools.org/api/docs/nearbySchools.page
XML :
<schools>
<school>
<gsId>936</gsId>
<name>Centerville Elementary</name>
<type>public</type>
<gradeRange>K-6</gradeRange>
...
</schools>
我这样创建 DOMain 用于转换
[Serializable()]
[System.Xml.Serialization.XmlRoot("schools")]
public class SchoolResponse
{
[XmlArray("schools")]
[XmlArrayItem("school", typeof(School))]
public School[] school { get; set; }
}
[Serializable()]
public class School
{
[System.Xml.Serialization.XmlElement("gsId")]
public string GSId { get; set; }
[System.Xml.Serialization.XmlElement("name")]
public string Name { get; set; }
....
}
调用方法:
var url = new Uri(this.BaseUri, request.ToUri());
return Internal.Http.Get(url,"XML").As<SchoolResponse>();
...
public virtual T As<T>() where T : class
{
T output = null;
using (var reader = GetStreamReader(this.RequestUri))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
output = (T)serializer.Deserialize(reader);
}
return output;
}
为空return。请帮忙
问题出在 school
数组属性上。试试这个
[Serializable()]
[System.Xml.Serialization.XmlRoot("schools")]
public class SchoolResponse
{
[XmlElement("school")]
public School[] school { get; set; }
}
通常 C# XML 喜欢有一个数组名称元素,然后每个数组值都有一个元素。通过在数组上使用 [XmlElement]
,不会生成外部元素。
如我所见 - 学校类型的有效值为:"public"、"charter"、"private" 或由连字符分隔的任何组合,如 "public-charter"
我认为最适合您的变体是这样声明枚举类型:
public enum SchoolType
{
[XmlEnum("public")]
Public,
[XmlEnum("charter")]
Charter,
[XmlEnum("private")]
Private,
[XmlEnum("public-charter")]
PublicCharter,
[XmlEnum("public-private")]
PublicPrivate,
[XmlEnum("private-charter")]
PrivateCharter,
[XmlEnum("public-charter-private")]
PublicCharterPrivate
}
那么您的 class 定义将如下所示:
[Serializable]
public class School
{
[XmlElement("gsId")] //Custom property name
public string GSId { get; set; } //The same property name
public string name { get; set; }
public string type { get; set; }
public string gradeRange { get; set; }
[XmlElement("schoolType")] //Enum declaration
public SchoolType SchoolType { get; set; }
}
但您也应该检查所有变体
我必须从 XML (greatschool API) 反序列化到 Class 域。
URL : http://www.greatschools.org/api/docs/nearbySchools.page
XML :
<schools>
<school>
<gsId>936</gsId>
<name>Centerville Elementary</name>
<type>public</type>
<gradeRange>K-6</gradeRange>
...
</schools>
我这样创建 DOMain 用于转换
[Serializable()]
[System.Xml.Serialization.XmlRoot("schools")]
public class SchoolResponse
{
[XmlArray("schools")]
[XmlArrayItem("school", typeof(School))]
public School[] school { get; set; }
}
[Serializable()]
public class School
{
[System.Xml.Serialization.XmlElement("gsId")]
public string GSId { get; set; }
[System.Xml.Serialization.XmlElement("name")]
public string Name { get; set; }
....
}
调用方法:
var url = new Uri(this.BaseUri, request.ToUri());
return Internal.Http.Get(url,"XML").As<SchoolResponse>();
...
public virtual T As<T>() where T : class
{
T output = null;
using (var reader = GetStreamReader(this.RequestUri))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
output = (T)serializer.Deserialize(reader);
}
return output;
}
为空return。请帮忙
问题出在 school
数组属性上。试试这个
[Serializable()]
[System.Xml.Serialization.XmlRoot("schools")]
public class SchoolResponse
{
[XmlElement("school")]
public School[] school { get; set; }
}
通常 C# XML 喜欢有一个数组名称元素,然后每个数组值都有一个元素。通过在数组上使用 [XmlElement]
,不会生成外部元素。
如我所见 - 学校类型的有效值为:"public"、"charter"、"private" 或由连字符分隔的任何组合,如 "public-charter" 我认为最适合您的变体是这样声明枚举类型:
public enum SchoolType
{
[XmlEnum("public")]
Public,
[XmlEnum("charter")]
Charter,
[XmlEnum("private")]
Private,
[XmlEnum("public-charter")]
PublicCharter,
[XmlEnum("public-private")]
PublicPrivate,
[XmlEnum("private-charter")]
PrivateCharter,
[XmlEnum("public-charter-private")]
PublicCharterPrivate
}
那么您的 class 定义将如下所示:
[Serializable]
public class School
{
[XmlElement("gsId")] //Custom property name
public string GSId { get; set; } //The same property name
public string name { get; set; }
public string type { get; set; }
public string gradeRange { get; set; }
[XmlElement("schoolType")] //Enum declaration
public SchoolType SchoolType { get; set; }
}
但您也应该检查所有变体