It's surprisingly tricky to get BinHex encoded data out of XmlDocument tree in .NET 1.X. XmlConvert class, which apparently has to support such conversions has a method FromBinHexString(), but weird - it only public for .NET Compact Framework. It's internal for regular .NET Framework. It's obvious that lots of .NET functionality isn't supported in .NET Compact Framework, but this is first time I see anything that .NET CF implements and .NET doesn't. Hmm, and in the forthcoming .NET 2.0 it's the same, do I miss something? Anyway, here is a workaround.
XmlTextReader can handle BinHex data just fine. So all you need is to get BinHex data out of XmlDocument along with containing element as string and read it with XmlTextReader:
XmlDocument doc = new XmlDocument();
doc.Load("foo.xml");
XmlNode dataNode = doc.SelectSingleNode("/data");
XmlTextReader r = new XmlTextReader(new StringReader(dataNode.OuterXml));
r.MoveToContent();
do
{
byte[] buf = new byte[1024];
int bytesRead = r.ReadBinHex(buf, 0, buf.Length);
string data = new string(Encoding.UTF8.GetChars(buf, 0, bytesRead));
Console.WriteLine(data);
} while (r.Name == "data");

Leave a comment