[英]How do I inject a string to NetworkStream so that it can be read via XmlReader.ReadAsync in C#?
I am reading XML data from the NetworkStream
like follows. 我正在从NetworkStream
读取XML数据,如下所示。
using (XmlReader xmlReader = XmlReader.Create(stream, new XmlReaderSettings() { Async = true }))
{
try
{
while (await xmlReader.ReadAsync())
{
...
I would like to call stream.Inject("</stop>")
so it can be read in the while
loop above. 我想调用stream.Inject("</stop>")
以便可以在上面的while
循环中读取它。 I know I would have to write a wrapper class around Stream
and implement the Inject
method and override the ReadAsync
method. 我知道我将不得不围绕Stream
编写包装类,并实现Inject
方法并重写ReadAsync
方法。 However I have no idea how to achieve that. 但是我不知道如何实现这一目标。 Would someone show me how something like that is done (I just can't imagine where in my Inject
method the additional string should be added)? 有人会告诉我如何完成此类操作(我无法想象应该在我的Inject
方法中的何处添加附加字符串)?
NetworkStream is a Stream so that means you can write any chunk of data into it, including XML strings. NetworkStream是一个Stream,这意味着您可以向其中写入任何数据块,包括XML字符串。 Of course, you have to do some conversion like: 当然,您必须进行一些转换,例如:
byte[] data = System.Text.Encoding.UTF8.GetBytes(longXmlString);
networkSteram.Write(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.