.NET を使用して WebAPI を作成しました。API は、xml ファイルからデータを読み書きします。次のコードがあり、ルート要素なしで一致する要素を返します。root で戻すにはどうすればよいですか?
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>