0

How do I transform XML into a binary file with some structure? I want to use XSLT, but this tehnology can not be used to convert xml to binary files.

What tehnology is acceptable for this problem?

4
  • 1
    What kind of binary file? Executable? If you want, you can just write the dang ASCII characters and pretend its binary structured. Commented Jun 16, 2011 at 9:48
  • What kind of binary file? Could you explain more what you are trying to achieve? What XML have you and what binary output do you want from it? Commented Jun 16, 2011 at 9:49
  • Without more detail of what you are trying to acheive, this question is completely pointless. Commented Jun 16, 2011 at 9:52
  • I have xml file with data, for example: <struct> <item> <id>5</id> <value>123</value> </item> <item> ..... </item> </struct> I must make binary file, where 4 bytes are id and next 4 bytes are value, for each item. This is example, in real file this structure is difficult and larger. Commented Jun 16, 2011 at 11:22

4 Answers 4

3

ASN.1.

Sign up to request clarification or add additional context in comments.

1 Comment

True :-) Seriously though, this is a solved problem. There's even a C# framework here: bnotes.sourceforge.net
0

Using a C# xml reading (using LINQ to xml for ease of use) and a BinaryWriter (to the correct format) could do the job. Could be a pain to get the output format correct though

Comments

0

That depends largely on what exactly the binary looks like ;p Xslt is not going to help much here; I would be using either a dedicated c# method that understands the structure (if it is specific to one task), or if a general purpose approach is needed (i.e. to cater for more than one scernario), then a proper serialization library to match your intended format.

You should be prepared to spend lots of time working with streams, buffers and encoders.

If you just want to serialize, and you don't care about the format - then use a pre-canned serializer; I suggest protobuf-net, as it works nicely alongside XmlSerializer allowing you to read from xml and write as binary, or read from binary and write as xml, etc. Likewise, since it is (by default) tree-based it has a direct correlation to xml (also tree-based).

Comments

0

If your data is not xml serialized then I would use an xml reader to read the data, create a strong typed class to represent your data then serialize it to binary, so when you read it back all you have to do is deserialize your binary file and cast it back to your orginal strongly typed class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.