split string into more xml documents
I have a C# client server application that communicates using xml documents.
The server sends to the client more xml documents one after other. In the client I read from socket as a buffer. The problem is that the client happens to read more xml documents at a time.
For example it happens to read a whole string like this:
<result>failed</result><other>more information</other>
How can I split the string into the two xml documen开发者_如何学JAVAts I need:
<result>failed</result>
and
<other>more information</other>
?
The simplest approach may well be to just add a wrapper:
string xml = "<wrapper>" + originalText + "</wrapper>";
Then you can read the whole thing into an XDocument
(or whatever) and find the individual elements under the wrapper.
There may well be smarter ways of doing this - see this MSDN article for example - but this one's probably the approach I'd take if it works for you, just on the grounds of it being insanely easy.
Note that if you need to be able to read some of the text without having read all of it, using an XmlReader
is probably a better solution - my approach requires you reading the whole thing into a string first. It's hard to tell whether or not this is appropriate without more details of the network protocol you're using.
精彩评论