开发者

Read binary XML as text in .NET

The AndroidManifest.xml file when compiled and packaged in the apk turns into a binary xml file. I give this information in case there is a difference between this one and regular binary xml files, but I'm not certain if there is.

I need to get information contained in this xml file. There are tools for java and python but I haven't found anything for .Net. How can I read this file in .NET?

This is the most promising solution I've found but it still doesn't display the text in a human readable format.

using (BinaryReader b = new BinaryReader(File.Open(filePath, FileMode.Open), Encoding.ASCII)) {
     int pos = 0;
     int length = (int)b.BaseStream.Length;
     while (pos < length) {
          char v = b.ReadChar();
          Console.Write(v);
          开发者_开发技巧pos += sizeof(char);
     }
}

Any ideas what I'm doing wrong ?

I've tried with different encodings when creating the BinaryReader and they haven't worked.

Thanks.


The file is not just "binary data" of the xml. There is a "format" to the android xml.

You can look at AXMLPrinter2 - it takes the binary xml and converts it to human readable. I Linked directly to a class that looks like it does the bulk of the work.

What information do you need from the manifest file? The aapt.exe in the android SDK can also pull all the info out of the android manifest file. You would just have to run that command (with the needed switches) and parse the console output stream.


The file looks like it is encoded in a custom format, I don't think there is a .NET library available for decoding it, so you'll have to rewrite a perl script like axml2xml.pl to do it for you. Unfortunately there isn't a quick-and-easy solution, but looking at it, rewriting it shouldn't be too hard.


Depending on your XML documents encoding, you can use the Encoding Class

System.Text.Encoding.ASCII.GetString(File.ReadAllBytes(filePath)); *or*
System.Text.Encoding.UTF8.GetString(File.ReadAllBytes(filePath)); *or*
System.Text.Encoding.UTF32.GetString(File.ReadAllBytes(filePath)); etc.

...to translate your byte arrays into human readable strings. Perhaps the reason your code above isn't displaying human readable characters is simply the encoding?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜