Click here to Skip to main content
15,880,405 members
Articles / Mobile Apps / Windows Mobile
Article

WBXML Support in C# - Handy

Rate me:
Please Sign up or sign in to vote.
3.21/5 (5 votes)
1 Nov 2007CPOL2 min read 38.4K   603   13   4
Open source class to handle WAP Binary XML in C# and CF C#

Introduction

Dear readers,

03 01 6A 00 00 01 41 42 03 57 42 58 4D 4C 20 50 72 6F 67 72 61 6D 6D 
65 72 00 01 43 03 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21 00 01 01 

Do you know what I'm talking about? The above 45 bytes string means the following 208 bytes XML:

XML
<hello-world> 
<greeter>WBXML Programmer</greeter> 
<greeting>Hello, World!</greeting> 
</hello-world>

Background

Basically, XML is a very easy language. It's easy to understand, parse and use. However, XML is very inefficient for transport. In order to send this small XML file, you have to transfer more than 200 bytes, when actually meaningful information is two strings. This is not critical issue when we are working in LAN environment. However it becomes very critical while working with mobile customers. That's the reason for inventing WAP Binary XML (or WBXML).

This language uses name tables (tokens) to convert nodes and attributes into one byte value and this way it saves a lot of unnecessary information being transferred. WBXML specification is very easy, however .NET (even Compact) framework's lack of XmlDocument supports this content format.

Is it common enough to lean and use? Well, this is tricky question. From one hand, all of mobile content providers (even those, who deal with mobile synchronization) widely use this format. On the other hand, if you are not really "in" mobile (or any other bandwidth efficient) programming, you do not need it.

I think that one of the most important things in programming (in general) is to be efficient, so I decided to write an XmlDocument derived class to provide WBXML support.

Using the Code

First create name tables (you do not have to if you are using only one token):

C#
Dictionary<int, Dictionary<byte, string>> tokens = 
	new Dictionary<int, Dictionary<byte, string>>(); 

Dictionary<byte, string> token = new Dictionary<byte, string>(); 
token.Add(1, "hello-world"); 
token.Add(2, "greeter"); 
token.Add(3, "greeting"); 

tokens.Add(1, token);

Then create a new WBXmlDocument instance with those tables:

C#
System.Xml.WBXmlDocument doc = new System.Xml.WBXmlDocument(tokens);

Now you can either use it as a normal XmlDocument...

C#
doc.LoadXml("<hello-world><greeter>WBXML Programmer</greeter>
	<greeting>Hello, World!</greeting></hello-world>");

... and then create its binary representation:

C#
byte[] bytes = doc.GetBytes();

Or load the incoming byte array...

C#
doc.Load(bytes);

... and get your XmlDocument:

C#
string xml = doc.DocumentElement.OuterXml;

That's all. Pretty easy, isn't it?

Points of Interest

The other implementation of WBXML in VB.NET can be found here on The Code Project.

Notice

  • This is not the final version.
  • It does not test enough.
  • There are known issues with attributes parsing.
  • It does not implement full specification (I did it for my own needs).
  • Each company might have its own protocol that can be slight different from official specification (SyncML for example).
  • You can know token tables only if you get full specification of target protocol. Private tokens are not transferred according to the protocol.

More information about this code licensing and terms of use can be found in my blog.

History

  • 1st November, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Better Place
Israel Israel
Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of course] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I used to work as a freelance architect, project manager, trainer, and consultant here, in Israel, but recently join the company with extremely persuasive idea - to make a world better place. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them.

To be updated within articles, I publishing, visit my blog or subscribe RSS feed. Also you can follow me on Twitter to be up to date about my everyday life.

Comments and Discussions

 
GeneralMy vote of 4 Pin
NetDave10-Feb-11 7:27
NetDave10-Feb-11 7:27 
GeneralErrors in demo project Pin
Member 47341873-Jan-08 21:27
Member 47341873-Jan-08 21:27 
GeneralRe: Errors in demo project Pin
MANOJ BATRA8-Feb-09 18:35
MANOJ BATRA8-Feb-09 18:35 
GeneralRe: Errors in demo project Pin
MANOJ BATRA8-Feb-09 18:35
MANOJ BATRA8-Feb-09 18:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.