Main siteMain site  ForumForum  ForumSearch  Private messageEmail contact  RegisterRegister  Log inLog in 
Topic: XMLNetServer data parsing
Reply to topic
Author Message
EMebane



Joined: Apr 15, 2019
Posts: 41

PostPosted: Jun 23, 2019 2:28 PM    Post subject: XMLNetServer data parsing

I'm working on receiving XML Socket data from BioEra in Unity. Most of the time BioEra sends more than one BioEraData node per tick. e.g., a single receipt from a Source Simulator element sent from BioEra to Unity looks like this:
<BioEraData Ch1="2.0" /><BioEraData Ch1="0.0" />

Parsing XML in Unity/C# is not really straightforward. The default parsing seems to expect a well-formatted XML document instead of individual nodes like we receive from BioEra. When I try prepending "<?xml version = \"1.0\" ?>" onto the beginning of each received packet to create a well-formatted XML document, the parser rejects it because it wants only one top-level node and it encounters more than one BioEraData node at the top level. The next step for me is to try nesting the contents one level deeper by wrapping a parent node around the BioEraData nodes, but it seems like there must be a better way.

Can anyone provide guidance for the best way of parsing the stream of BioEraData nodes in C# or any language that may have equivalent techniques in C#?
EMebane



Joined: Apr 15, 2019
Posts: 41

PostPosted: Jun 23, 2019 2:43 PM    Post subject:

Update:
Wrapping the data in a parent node seems to work, but I'm concerned about the reliability of this approach.

Here's a snippet of how I prepare the incoming XML packets for parsing:
"<?xml version = \"1.0\" ?><data>" + Encoding.ASCII.GetString( incomingData ) + "</data>";
jarek



Joined: Oct 22, 2007
Posts: 698

PostPosted: Jun 23, 2019 4:49 PM    Post subject:

Using a full XML parser is in my opinion an overkill. The format is so simple and consistent, it might be easier to parse it character by character.

Here is a simple C# example (fully open source):

http://proatech.com/sdk.html

This C# project was created a long time ago, so I am not sure it still can be directly imported into a new version of Visual Studio. But the relevant parsing code only takes a few lines in XmlNetServer.cs, see the Read method.
EMebane



Joined: Apr 15, 2019
Posts: 41

PostPosted: Jun 23, 2019 9:45 PM    Post subject:

Here's the relevant method from the XmlNet.cs class you provided:

protected string getAttribute(string section, string name) {
int i = section.IndexOf(name + "=\"") + name.Length + 2;
int j = section.IndexOf("\"", i);

//Console.WriteLine("Attribute i=" + i + " j=" + j + " for " + name + " in " + section);

if (i < 0 || j < 0)
return null;

return section.Substring(i, j - i);
}

It uses a principle of searching for contents between =" and ". You could apply that principle in a loop to find values when multiple nodes are receive in the same tick. You could also search for BioEraData or BioEraEvent and filter the different kinds of receipts from BioEra.
jarek



Joined: Oct 22, 2007
Posts: 698

PostPosted: Jun 23, 2019 10:34 PM    Post subject:

Yes, exactly. The C# example does this for at least 1 channel (I don't remember if more channels was implemented there in that example) and shows that value in real time.

That is a simple but very efficient way of parsing (much less processing power needed than using a full XML parser).
Reply to topic