XML Reading Problem  
Author Message
Rosbief





PostPosted: XML and the .NET Framework, XML Reading Problem Top

Hi,
I'm loving what you all can do with C# but i'm currently new at C# with the use of XML.
I'm making a project and i'm saving info in a XML file since for me its the best choice for storage because i don't wanne use access databases or SQL or MYSQL.

So i'm trying a bit but i'm stuck now.
I got a bit code to read a xml file well its rather a long and simple way but it does.
But my error is that i get this error,

System.Xml.XmlException: 'Element' is an invalid XmlNodeType. Line 8, position 4.
at System.Xml.XmlReader.ReadEndElement()
at PlayGround.Form1.button1_Click(Object sender, EventArgs e) in C:\Coding\C#\PlayGround\PlayGround\Form1.cs:line 36
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam

My code looks like this:

XmlTextReader READ01 = new XmlTextReader("Data/Config.xml");

READ01.Read();

READ01.ReadStartElement("XML");
   
    READ01.ReadStartElement("GENERAL");

        READ01.ReadStartElement("VERSION");
            textBox1.Text = "Version:" + READ01.ReadString() + "\r\n";
        READ01.ReadEndElement();
    READ01.ReadEndElement();

    READ01.ReadStartElement("USERS");
        READ01.ReadStartElement("TOTAL");
            textBox1.Text = "Total Users:" + READ01.ReadString() + "\r\n";
        READ01.ReadEndElement();
    READ01.ReadEndElement();

READ01.ReadEndElement();

MY XML File:

<XML>
    <GENERAL>
        <VERSION>10</VERSION>
    </GENERAL>
   
    <USERS>
        <TOTAL>5</TOTAL>
        <CURRENT>Gudrun</CURRENT>
    </USERS>
</XML>

Can anyone please guide me in the right direction, i would be very pleased :p


.NET Development10  
 
 
He Man





PostPosted: XML and the .NET Framework, XML Reading Problem Top


I would always suggest to use the XPathDocument class for read only XML documents.

You forgot to bypass the <CURRENT> node.

Use this snippet:

XmlTextReader READ01 = new XmlTextReader("Data/Config.xml");

READ01.Read();

READ01.ReadStartElement("XML");

READ01.ReadStartElement("GENERAL");

READ01.ReadStartElement("VERSION");
textBox1.Text = "Version:" + READ01.ReadString() + "\r\n";
READ01.ReadEndElement();
READ01.ReadEndElement();

READ01.ReadStartElement("USERS");
READ01.ReadStartElement("TOTAL");
textBox1.Text = "Total Users:" + READ01.ReadString() + "\r\n";
READ01.ReadEndElement();

// Bypass the <CURRENT> Node

READ01.ReadStartElement("CURRENT");
READ01.ReadString();
READ01.ReadEndElement();
READ01.ReadEndElement();

READ01.ReadEndElement();