使用 tcp 套接字在 Windows Phone 7 上解析 xml 内容 [英] parsing xml content on windows phone 7 using tcp sockets

查看:35
本文介绍了使用 tcp 套接字在 Windows Phone 7 上解析 xml 内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发适用于 Windows Phone 7 的应用程序.

I am working on an application for windows phone 7.

我有一个在 c# silverlight 中使用 tcp 套接字解析 xml 流的方法.我正在尝试使用 xmlreader 和内存流,但它没有帮助.当内存流被接收异步调用更新时,xmlreader 对该读取器没有影响.

I have a to parse xml stream using tcp sockets in c# silverlight. I am trying it using xmlreader and memory stream but it is of no help. When memory stream is updated by a receive async call, xmlreader has no impact of that reader.

请帮助我了解如何从套接字解析流式 xml.

Please help me on how to parse streaming xml from sockets.

我有一个 xmlReader,这样:

I have a xmlReader such that:

memoryStream= new MemoryStream();   
_xmlreader = XmlReader.Create(memoryStream, xmlReaderSettings, context);

现在 memoryStream 更新为:

now memoryStream is updated as:

byte []buffer = "initialized with some xml bytes such as <node1> data </node1>"

因为这个缓冲区是由套接字接收异步操作填充的,它是 xml.现在我需要更新我的数据.所以我这样做...

as this buffer is filled by socket receiveasync operation which is xml. now i need to update the my data. so I do this...

memoryStream = memoryStream.write(buffer,0,buffer.length);

memoryStream = memoryStream.write(buffer,0,buffer.length);

现在当我这样做时 _reader.read 失败了.我不明白为什么会这样.否则是否有 xmlpullparser (sax) 之类的东西,就像我们在 android os 中用于 xml 解析一样

Now when i do this _reader.read fails. I don't why is this happening. otherwise is there is xmlpullparser (sax) like thing as we have in android os for xml parsing

while (_reader.Read())
            {
                switch (_reader.NodeType)
                {
                    case XmlNodeType.Element:
                        {
                            node = new XElement(_reader.Name);
                            xmlBuildStack.Push(node);

                        }
                        break;

                    case XmlNodeType.EndElement:
                  .....

有没有其他方法可以解析来自 tcp 套接字流的 xml,因为我正在开发使用 xmpp xml 节的聊天应用程序.请帮我解决这个问题.

is there any other way possible to parse xml which comes from tcp socket stream as i am working on chat application which uses xmpp xml stanzas. please help me in solving this scenario.

推荐答案

MemoryStream 无法按您希望的方式工作.当 XmlReader 读取到当前末尾(即消耗所有当前数据)时,它将报告它已到达文件末尾.

MemoryStream doesn't work the way you want it to. When the XmlReader reads to the current end (i.e. consumes all of the current data), it's going to report that it's reached end of file.

您真正需要的是在 TCP 套接字周围包裹一个流.可能最简单的方法是使用 TcpClient 而不是原始套接字.GetStream 方法将执行您想要的操作——提供一个流,您可以从中创建一个 XmlReader.

What you really need is to wrap a stream around the TCP socket. Probably the easiest thing to do would be to use a TcpClient rather than a raw socket. The GetStream method will do what you want--provide a stream from which you can create an XmlReader.

另一种选择是创建一种可以满足您要求的流.也就是说,它让一个线程放入数据,另一个线程取出数据.但是在生产者文件结束之前,消费者不会报告文件结束.

Another option is to create a type of stream that does what you're asking. That is, it lets one thread put data in and another take data out. But the consumer doesn't report end of file until the producer says end of file has occurred.

几个月前我写并发表了这样的文章.请参阅构建新型流.

I wrote and published something like this a few months back. See Building a New Type of Stream.

老实说,在这种情况下,TcpClient 将是我的首选.

Honestly, though ... TcpClient would be my preference in this case.

这篇关于使用 tcp 套接字在 Windows Phone 7 上解析 xml 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆