0

I want to build C# binary decoder. I have XML file which is describing data structure of binary file.

Next step is dynamically making data structures (in code) based on that XML. Do you guys/girls have any comments, links, code, etc for me?

I am aware that this is general question, but I just want to start some where and I don't have clue from where.

EDIT:

Sorry need to remove code...

BR

JohnDoeKazama
  • 153
  • 3
  • 12
  • you got two questions. One which is a bit too broad (how to decode binary information) and the other is how to read XML files. You should split the question into two and provide more information for each specific question. – jgauffin Feb 27 '13 at 12:02
  • Yes I'm aware of that. The thing is that question is connected. I need to write code which on base on XML build C# struct or classes. Reading XML is not problem, there are many and easy ways to do that, but trick is how can I generate structs or classes from that XML (in code). – JohnDoeKazama Feb 27 '13 at 12:09
  • No, they are not really related. You can read the XML and create C# classes which contains instructions on how to build the objects. The XML part is just serialization of the instructions and not really part of the generation question. You still need to provide what the XML contains? Class names/Property names? Or is it data which you should use to try to figure out the data types? – jgauffin Feb 27 '13 at 12:22
  • This last. my xml file contain data which is describing data types (byte, ushort (here I have little endian problem), etc). SO my xml file saying: this is header, this is event No 1, this is optional parameter etc. Please look in this my question (Sorry for duplicating questions): http://stackoverflow.com/questions/15020696/c-sharp-binary-decoder-based-on-xml-specification – JohnDoeKazama Feb 27 '13 at 12:45

2 Answers2

1

Since you want to dynamically create the structures (rather than statically build classes based upon the XML definition), I guess you need a generic data structure which you can then query. What this looks like exactly would depend on what kind of data structures you're describing. Is it records and fields? Are there multiple record types in a hierarchy? If there's no hierarchy, you could just use a dictionary of key-value pairs for each field. If there is hierarchy, I'd have thought a navigable tree would cover most scenarios. You could use an XML DOM for this, but I think that's not the cleanest solution and I prefer to use generic tree structures. There isn't a built in one (see Tree data structure in C#), but it's fairly easy to create one with generics.

EDIT

The above assumes you want to dynamically create a structure to be used dynamically. If you want to dymanically create a structure in code that will be used statically (e.g. you want to be able to write something like myDataStrucureThatWasDefinedInXml.MyProperty1), have a look at CodeDom.

And having thought some more about it, it really depends on what you want to do once you've deserialized your binary data. You might also want to look at the Expando object and Expression Trees.

Community
  • 1
  • 1
Giles
  • 1,331
  • 1
  • 15
  • 30
  • Hi, this is very interesting answer. The key problem is that I have this xml file which is decribing how to decode and parse data (eg: first come header which contain 9 parameters of which this two are optional, etc.) and this XML **will change** in future. EDIT: There is also xsd file which is desc of my xml file (but xml and binary file are input to application). – JohnDoeKazama Feb 27 '13 at 12:52
  • 1
    If the XML is going to change, I presume what you're doing with the data is fairly generic and not reliant on certain piece of data being there? Otherwise, the code that processes your deserialized data is going to need to be changed each time the XML changes. If so, it sounds like you need the generic data structure approach. – Giles Feb 27 '13 at 13:01
  • 1
    I see from the example code you've added that you're trying to read directly into the structure (I didn't realise this was in memory). Since you haven't got a precise structure because it's defined in XML, you'll have to read the binary data not as a structure, but just as a byte array. Then, based upon you're XML definition, you'll have to interpret that array (and put it in a generic structure or dynamic object, etc). – Giles Feb 27 '13 at 13:22
  • Hi, this is unsafe is code because of unsafe struct. This is just my first example. So, inputs to app are: binary file (multiple number of files), XML file which describing types of data, structures etc (xml file is somehow confidential :( )and optionally I have xsd of this file as input (if it's useful somehow) – JohnDoeKazama Feb 27 '13 at 13:31
  • 1
    You can use Stream.Read, passing in the number of bytes to read (as defined in your XML file) and a byte array to put the data in. You can then take the data out of that byte array (as further defined in your XML file) and put it in a generic data structure. – Giles Feb 27 '13 at 13:47
0

To read and write content of XML document you can use XmlDocument class provided in C#.

Here are some links, which can help you:

jgauffin
  • 99,844
  • 45
  • 235
  • 372
Vahagn Nahapetyan
  • 1,337
  • 13
  • 22
  • Link only answers are not allowed here. You should extract a code snippet which shows how the task is performed. – jgauffin Feb 27 '13 at 12:01
  • Hi, thx for answer, but reading XML is not problem at all. Problem is how to generate structs or classes from that xml in code. – JohnDoeKazama Feb 27 '13 at 12:09