Wednesday, November 10, 2010

C# Tutorial: Load Text from XML File [Load Settings]

Here is the code

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
XmlTextReader xreader = new XmlTextReader(openFileDialog1.FileName);
string elem = ""; int ok = 0;
while (xreader.Read())
{
    switch (xreader.NodeType)
    {
        case XmlNodeType.Element:
            {
                if (xreader.Name == "Title1")
                    elem = "Title1";
                else if (xreader.Name == "Title2")
                    elem = "Title2";
            }break;
        case XmlNodeType.Text:
            {
                if (elem == "Title1")
                {
                    if (ok == 0)
                    {  label1.Text = xreader.Value; ok=1;}
                    else listBox1.Items.Add(xreader.Value);
                }
                else if (elem == "Title2")
                {
                    if (ok == 1)
                    { label2.Text = xreader.Value; ok = 0; }
                    else listBox2.Items.Add(xreader.Value);
                }
            }break;
    }
}
xreader.Close();
}

No comments:

Post a Comment