好吧,既然这个问题是风滚草,我会发布我自己找到的答案......
我发现这个(除其他外)有助于 .NET XML 序列化/反序列化的基础。我尝试使用xsd.exe
,但结果令人失望。但是从这个基础开始,我为 XML 提要创建了一个 CLR 类型:
[XmlRoot("search")]
public class SearchTag
{
[XmlAttribute("ver")]
public string ver { get; set; }
[XmlElement("loc")]
public SearchLocTag[] loc { get; set; }
}
public class SearchLocTag
{
[XmlAttribute("id")]
public string id { get; set; }
[XmlAttribute("type")]
public string type { get; set; }
[XmlText]
public string text { get; set; }
}
XMLMediaTypeFormatter 使用的默认序列化程序 (DataContractSerializer) 对标记名称做了不希望的事情,并插入了不需要的 XmlSerializerNamespaces 信息。但我发现了这一点并创建了一个自定义 XmlObjectSerializer:
public class SearchSerializer : XmlObjectSerializer
{
XmlSerializer serializer;
public SearchSerializer()
{
this.serializer = new XmlSerializer(typeof(SearchTag));
}
public override void WriteObject(XmlDictionaryWriter writer, object graph)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(writer, graph, ns);
}
public override bool IsStartObject(XmlDictionaryReader reader)
{
throw new NotImplementedException();
}
public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
{
throw new NotImplementedException();
}
public override void WriteEndObject(XmlDictionaryWriter writer)
{
throw new NotImplementedException();
}
public override void WriteObjectContent(XmlDictionaryWriter writer, object graph)
{
throw new NotImplementedException();
}
public override void WriteStartObject(XmlDictionaryWriter writer, object graph)
{
throw new NotImplementedException();
}
}
然后将其添加到 Global.asax.cs:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer<SearchTag>(new SearchSerializer());
然后控制器变成了:
public class SearchController : ApiController
{
public HttpResponseMessage Get(string location)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://wxdata.weather.com");
HttpResponseMessage response = client.GetAsync(String.Format("wxdata/search/search?where={0}", location)).Result;
response.EnsureSuccessStatusCode();
var stream = response.Content.ReadAsStreamAsync().Result;
var serializer = new XmlSerializer(typeof(SearchTag));
using (var reader = new XmlTextReader(stream))
{
if (serializer.CanDeserialize(reader))
{
var xmlData = (SearchTag)serializer.Deserialize(reader);
return Request.CreateResponse<SearchTag>(HttpStatusCode.OK, xmlData);
}
else
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
}
}
}
生成的应用程序/xml 消息:
要求
GET http://localhost:53047/twc/search/london HTTP/1.1
User-Agent: Fiddler
Accept: application/xml
Host: localhost:53047
回复
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcQ2xheXRvblxEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEzXFByb2plY3RzXFR3Y0FwaVx0d2Ncc2VhcmNoXGxvbmRvbg==?=
X-Powered-By: ASP.NET
Date: Wed, 04 Dec 2013 21:42:46 GMT
Content-Length: 484
<search ver="3.0"><loc id="UKXX0085" type="1">London, GLA, United Kingdom</loc><loc id="USAL2905" type="1">London, AL</loc><loc id="USAR0340" type="1">London, AR</loc><loc id="USCA9301" type="1">London, CA</loc><loc id="USIN2109" type="1">London, IN</loc><loc id="USKY1090" type="1">London, KY</loc><loc id="USMI2014" type="1">London, MI</loc><loc id="USMN2182" type="1">London, MN</loc><loc id="USMO2769" type="1">London, MO</loc><loc id="USOH0520" type="1">London, OH</loc></search>
以及由此产生的应用程序/json 消息:
要求
GET http://localhost:53047/twc/search/london HTTP/1.1
User-Agent: Fiddler
Accept: application/json
Host: localhost:53047
回复
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcQ2xheXRvblxEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEzXFByb2plY3RzXFR3Y0FwaVx0d2Ncc2VhcmNoXGxvbmRvbg==?=
X-Powered-By: ASP.NET
Date: Wed, 04 Dec 2013 21:43:18 GMT
Content-Length: 528
{"ver":"3.0","loc":[{"id":"UKXX0085","type":"1","text":"London, GLA, United Kingdom"},{"id":"USAL2905","type":"1","text":"London, AL"},{"id":"USAR0340","type":"1","text":"London, AR"},{"id":"USCA9301","type":"1","text":"London, CA"},{"id":"USIN2109","type":"1","text":"London, IN"},{"id":"USKY1090","type":"1","text":"London, KY"},{"id":"USMI2014","type":"1","text":"London, MI"},{"id":"USMN2182","type":"1","text":"London, MN"},{"id":"USMO2769","type":"1","text":"London, MO"},{"id":"USOH0520","type":"1","text":"London, OH"}]}
这正是我所追求的。