Main Site Documentation

Reading larger XML files


i want to parse a large xml file (50kbyte) so I cannot load it into memory completely before parsing.
Now I modified your sample code from the beginners guide (p.132-133) but I always get a “System.Xml.XmlException” when trying to read the file at the xmlr.Read();.
My code:

System.IO.FileStream xmfile = File.Open(filename, FileMode.Open);

            XmlReader xmlr = XmlReader.Create(xmfile);
                while (!xmlr.EOF)
                    switch (xmlr.NodeType)
                        case XmlNodeType.Element:
                            Debug.Print("element: " + xmlr.Name);
                        case XmlNodeType.Text:
                            Debug.Print("text: " + xmlr.Value);
                        case XmlNodeType.XmlDeclaration:
                            Debug.Print("decl: " + xmlr.Name + ", " + xmlr.Value);
                        case XmlNodeType.Comment:
                            Debug.Print("comment " + xmlr.Value);
                        case XmlNodeType.EndElement:
                            Debug.Print("end element");
                        case XmlNodeType.Whitespace:
                            Debug.Print("white space");
                        case XmlNodeType.None:
            catch (Exception exe)
                string s = exe.Message;
                return false;

The file exists on the SD card, the filesystem is mounted and the FileInfo shows that it can access the file.
What am I doing wrong? Or is there another way to stream an XML file from SD card without having to load all text into memory?



Is there something in the XML that NETMF doesn’t like? Can you shrink the same file, load it complete in RAM and try to read it? Then if it works, place same file on SD and try to read it.


Thx… I will try it.
The Problem is already at the first read command… Maybe the XML header is not accepted and the exception is thrown or there is another problem.
I will report my progress.


do you have any details on the xml exception? it should show up in your output window


Are you pointing to the SD card?


It seems to be a problem in the XML file itself: the first line was:

<?xml version="1.0" encoding="ISO-8859-1"?>

and after that there is the start tag. If I remove this first line it starts to parse correctly.
After that I also get a lot of XMLExceptions with hint to CLR_IO failures but when I remove comments and other stuff then it works after inserting:

XmlReaderSettings ss = new XmlReaderSettings();
            ss.IgnoreWhitespace = true;
            ss.IgnoreComments = true;
            XmlReader xmlr = XmlReader.Create(xmfile, ss);

It seems the XMLReader is very sensitive in the Microframework. On .NET desktop version it parses fine without any modification.


Everything on netmf is very small buy if you can provide a small XML file that shows the issue then we can look into it


I’ve never had any trouble at all reading XML on NETMF. If you use a tool like XML NotePad it will let you know if your XML has issues so you can correct them.


There is no problem any more if I keep my XML file simple after removing the fancy start tag attributes. The version that does not work was:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Ruleset xmlns:xsi="" xsi:noNamespaceSchemaLocation="rules.xsd">

Now i have the problem that reading the XML file will at some time create an out-of-memory exception caused by reading a lot of data even when i do nothing while parsing… just reading.
Why is the XML Reader taking so much memory by just parsing (reading) the file?
It is impossible to parse a 20 kbyte file this way.

Is there a problem in the implementation of the XMLReader? Do i have to write my own parser?


I noticed that the header you have on the xml file

<?xml version="1.0" encoding="ISO-8859-1"?>

is different from the one in the example

<?xml version="1.0" encoding="utf-8"?>