parsing xml content on windows phone 7 using tcp sockets

删除回忆录丶 提交于 2020-01-01 19:56:08

问题


I am working on an application for windows phone 7.

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.

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

I have a xmlReader such that:

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

now memoryStream is updated as:

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

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);

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:
                  .....

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.


回答1:


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.

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.

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




回答2:


XmlReader is nearly useless for XMPP, unfortunately. It buffers to 4kB before processing any input, regardless of the stream you hook up to it.

Even as a first hack to prove this to yourself, you shouldn't write a stream that pads every inbound stanza to 4kB, but ONLY on stanza boundaries.

To get around the problem, I ported the parts of James Clark's XP parser to C#. If LGPL works for you, you could pull xpnet out of jabber-net (which I haven't updated to support WinPhone7), or send me patches if you get the entire library working.



来源:https://stackoverflow.com/questions/8700943/parsing-xml-content-on-windows-phone-7-using-tcp-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!