1

I have a device that communicates over a persistent TCP socket using XML. Commands are sent to it in XML structure and responses are returned in XML form. The device outputs a root element open such as <OurProtocol> when connection is made. Each response to commands or query will return unique XML fragments. I have the following code to parse the stream into nodes with XMLTextReader.

deviceClient.Connect(ipRemoteEndPoint);
deviceStream = deviceClient.GetStream();

XmlTextReader reader = null;
reader = new XmlTextReader(deviceStream);
while (reader.Read())
{
   switch (reader.NodeType)
   {
      case XmlNodeType.Element: // The node is an Element.
      ....
   }
}

I was planning on building a state machine to process the nodes as they are detected; with the final destination into C# data classes for consumption by the main program. Is there existing .NET technology that can process the nodes and save me from hand coding state machines for processing the returned XML fragments?

I do have access to a schema file, but thought I couldn't use documemt classes since the source of data is not a full string or file but is instead a network stream.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Welcome to Stack Overflow! FYI, you should not use `new XmlTextReader()` or `new XmlTextWriter()`. They have been deprecated since .NET 2.0. Use `XmlReader.Create()` or `XmlWriter.Create()` instead. – John Saunders Oct 09 '13 at 18:52
  • Look at [LINQ to XML](http://msdn.microsoft.com/en-us/library/bb387098.aspx). That's the easy way to process XML. – John Saunders Oct 09 '13 at 18:53
  • Thanks for the quick response. Those comments on XMLTextReader for NET 2.0 read to me like what you should do if you are using the NET 2.0 Framework. But it is easy enough to change. As for LINQ to XML, I would like to use it, but can I really do so with a NetworkStream? – MasterOfNone Oct 09 '13 at 19:30
  • Which version of .NET are you using? – John Saunders Oct 09 '13 at 19:31
  • Using Net 3.5, so have access to LINQ. More worried about stream timing and not having complete well formed XML fragment in when doing call to LINQ. – MasterOfNone Oct 09 '13 at 19:40
  • Ok, just re-read your question, and I think you're stuck with XmlReader. Just use `XmlReader.Create(stream)` instead of `new XmlTextReader(stream)`. You might find a way to use `XmlReader` to read forward until the last node of a fragment, then parse a fragment at a time via `XDocument`. I'm not sure how you would do this, though. – John Saunders Oct 09 '13 at 19:49

0 Answers0