tags:

views:

84

answers:

2

Hi,

I've been developing a small xml parser for a specific file with this data structure :

<questionnaire type="Echo">
    <quest etat="0" ord="0">
        <intro>
            <t>bla bla bla</t>
        </intro>
        <rep1>
            <t>word</t>
            <ev id="Q0R1"/>
        </rep1>
        <rep2>
            <t>word</t>
            <ev id="Q0R2"/>
        </rep2>
    </quest>
</questionnaire>

The <quest></quest> tag is repeated 70 times in fact.

Here is the parser I developed in C# and which takes the parameter "xmlString" containing the XML to parse :

XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(xmlString));

while(reader.ReadToFollowing("quest"))
{
    Console.WriteLine("=================================");

    Console.WriteLine("ID : " + reader.GetAttribute("ord").ToString());

    reader.ReadToFollowing("intro");
    reader.ReadToFollowing("t");
    Console.WriteLine("TEXTE : " + reader.ReadString());

    reader.ReadToFollowing("rep1");
    reader.ReadToFollowing("t");
    Console.WriteLine("REPONSE1 : " + reader.ReadString());
    reader.ReadToFollowing("ev");
    Console.WriteLine("CLE1 : " + reader.GetAttribute("id").ToString());

    reader.ReadToFollowing("rep2");
    reader.ReadToFollowing("t");
    Console.WriteLine("REPONSE2 : " + reader.ReadString());
    reader.ReadToFollowing("ev");
    Console.WriteLine("CLE2 : " + reader.GetAttribute("id").ToString());

    Console.WriteLine("ETAT : False");
}

Everything is ok through the 69 first < quest >, but when reaching the 70th, it the fields TEXTE and REPONSE1 are empty and it goes through a NullReferenceException at the line :

Console.WriteLine("CLE1 : " + reader.GetAttribute("id").ToString());

Can anybody tell me what's wrong ?? Thanks in advance.

A: 

GetAttribute returns null if the attribute does not exist for the current node. So, doing ToString() on a null object throws the NullReferenceException.

To fix it, check for a null value before doing ToString().

By the way, GetAttribute already returns a string so it pretty useless to call ToString.

Francis B.
Ok, thank you !I first removed the .ToString() calls and I don't get any error now.BUT the last read of my loop only fill the first element (ID) and the others are empty ... really weird ??
A: 

Why don't you use XPath?

XPathDocument doc = new XPathDocument("sample.xml");
var navigator = doc.CreateNavigator();
var iterator = navigator.Select("/questionnaire/quest");
while (iterator.MoveNext())
{
    Console.WriteLine("=================================");

    Console.WriteLine("ID : " + iterator.Current.GetAttribute("ord", String.Empty));
    var intro = iterator.Current.Select("intro/t");
    if (intro.MoveNext())
    {
     Console.WriteLine("TEXTE : " + intro.Current.Value);
    }

    var response1 = iterator.Current.Select("rep1/t");
    if (response1.MoveNext())
    {
     Console.WriteLine("REPONSE1 : " + response1.Current.Value);
    }

    var cle1 = iterator.Current.Select("rep1/ev/@id");
    if (cle1.MoveNext())
    {
     Console.WriteLine("CLE1 : " + cle1.Current.Value);
    }

    var response2 = iterator.Current.Select("rep2/t");
    if (response2.MoveNext())
    {
     Console.WriteLine("REPONSE2 : " + response2.Current.Value);
    }

    var cle2 = iterator.Current.Select("rep2/ev/@id");
    if (cle2.MoveNext())
    {
     Console.WriteLine("CLE2 : " + cle2.Current.Value);
    }

    Console.WriteLine("ETAT : False");
}
Bryan
Ok !!! That works perfectly !!Thanks a lot Bryan