4
我已經使用.NET創建了WebAPI。 API從xml文件讀取/寫入數據。我有以下代碼,它返回沒有根元素的匹配元素。我如何使它以root身份返回?用XML數據返回HttpResponseMessage
API控制器:
[HttpGet]
public HttpResponseMessage GetPerson(int personId)
{
var doc = XDocument.Load(path);
var result = doc.Element("Persons")
.Elements("Person")
.Single(x => (int)x.Element("PersonID") == personId);
return new HttpResponseMessage() { Content = new StringContent(string.Concat(result), Encoding.UTF8, "application/xml") };
}
結果:
<Person>
<PersonID>1</PersonID>
<UserName>b</UserName>
<Thumbnail />
</Person><Person>
<PersonID>2</PersonID>
<UserName>b</UserName>
<Thumbnail />
</Person><Person>
<PersonID>4</PersonID>
<UserName>a</UserName>
<Thumbnail>a</Thumbnail>
</Person>
謝謝,成功了! – tempid 2013-04-22 18:02:23