[Monodroid] How To Retrieve XML Resources

Dan Russell danomania at gmail.com
Fri Nov 19 00:13:15 EST 2010


On Thu, Nov 18, 2010 at 12:57 PM, Jonathan Pryor <jpryor at novell.com> wrote:

> Two notes:
>  1. I suspect that resources within Resources\xml are NOT accessible
>    via Resources.OpenRawResource().  Resources.OpenRawResource() is
>    for files within Resources\raw.

It turns out that they are accessible. The InputStream stream returned by
Resources.OpenRawResource() has the content of the XML data. It is just that
it is compiled and Resources.OpenRawResource() does not decompile it. It's
in its raw form. I verified this by doing the following:

1) Added the obj\Debug\android\
AndroidManifest.xml file and added it to a project as an AndroidResource in
2) In the project, loaded the resource using Resources.OpenRawResource() and
saved it to a file on the device named resource_compiled.xml
3) Pulled the resource_compiled.xml file from the device to my local file
4) Opened the project .apk file in WinZip and extracted the
AndroidManifest.xml file
5) Compared the resource_compiled.xml and AndroidManifest.xml files and
observed that they are exact matches.

It appears that the only way to decompile these files is by calling one of
the AssetManager.openXmlResourceParser() methods, one of which is called by
the Resources.getXml() method. In the Android source for
AssetManager.openXmlResourceParser(), a call is made to the non-public
method **AssetManager.openXmlAssetNative() which as the name implies calls a
native method that does the actual work.

The only use case that I can think of where exposing these methods is where
you would want to share resources among Android and .NET APIs. I suspect
this would be rare, but a remote possibility.

>    It would be interesting to know what error you're getting, as
>    I would expect it to fail via an exception.

System.Xml.XmlException: Invalid data

   Pity they don't mention XML resources. :-/


> 2. Mike had suggested using .NET resources.  So the (not so) obvious
>    solution is to embed your XML as a managed resource
>    (`CSC /resource:file.xml ...`), then use
>    Assembly.GetManifestResourceStream() to load it.  This has the
>    added advantage of being portable across different .NET platforms.

This works perfectly. Thanks.

However, while in the midst of testing this, I discovered another issue. If
the XML resource contains an embedded DTD, an exception is thrown because it
is apparently not implemented.

E/mono    (12525): [0xafd48828:] EXCEPTION handling: System.
NotImplementedException: The requested feature is not implemented.
E/Mono.Android(12525): System.NotImplementedException: The requested feature
is not implemented.
E/Mono.Android(12525):   at System.Xml.DTDReader.ReadAttributeDefinition ()
[0x00000] in

E/Mono.Android(12525):   at System.Xml.DTDReader.ReadAttListDecl ()
[0x00093] in

E/Mono.Android(12525):   at System.Xml.DTDReader.CompileDeclaration ()
[0x0016f] in

E/Mono.Android(12525):   at System.Xml.DTDReader.ProcessDTDSubset ()
[0x000d9] in

E/Mono.Android(12525):   at System.Xml.DTDReader.GenerateDTDObjectModel ()
[0x0007e] in

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20101118/1290173b/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: XmlResourceSandbox.zip
Type: application/zip
Size: 19248 bytes
Desc: not available
Url : http://lists.ximian.com/mailman/private/monodroid/attachments/20101118/1290173b/attachment-0001.zip 

More information about the Monodroid mailing list