我有以下方法签名。我无法更改它(即我无法更改返回类型)。
public Stream GetMusicInfo(string songId)
{
XElement data = dao.GetMusicInfo(songId);
// how do I stream the XElement?
}
如何使用 WCF 流式传输 XElement/XDocument?
最佳答案
如果您不介意实际上获取第一行中的所有数据,那么这相当简单:
public Stream GetMusicInfo(string songId)
{
XElement data = dao.GetMusicInfo(songId);
MemoryStream ms = new MemoryStream();
data.Save(ms);
ms.Position = 0;
return ms;
}
换句话说,只需将其写到内存中,然后通过内存中的表示形式返回一个流。请注意 Position = 0;
调用,这是必需的,否则流将位于数据的末尾。
我希望 WCF 能够对流做正确的事情。
关于c# - 如何使用 WCF 流式传输 XElement/XDocument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7576422/