Main Site Documentation

Reading larger XML files


#1

Hi,
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);
            try
            {
                while (!xmlr.EOF)
                {
                    xmlr.Read();
                    switch (xmlr.NodeType)
                    {
                        case XmlNodeType.Element:
                            Debug.Print("element: " + xmlr.Name);
                            break;
                        case XmlNodeType.Text:
                            Debug.Print("text: " + xmlr.Value);
                            break;
                        case XmlNodeType.XmlDeclaration:
                            Debug.Print("decl: " + xmlr.Name + ", " + xmlr.Value);
                            break;
                        case XmlNodeType.Comment:
                            Debug.Print("comment " + xmlr.Value);
                            break;
                        case XmlNodeType.EndElement:
                            Debug.Print("end element");
                            break;
                        case XmlNodeType.Whitespace:
                            Debug.Print("white space");
                            break;
                        case XmlNodeType.None:
                            Debug.Print("none");
                            break;
                        default:
                            Debug.Print(xmlr.NodeType.ToString());
                            break;
                    }
                }
            }
            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?

Sincerely
Holger


#2

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.


#3

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.
Holger


#4

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


#5

Are you pointing to the SD card?


#6

Hi,
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.
Holger


#7

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


#8

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.


#9

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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="rules.xsd">
.....
</Ruleset>

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?
Holger


#10

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"?>