Hello guys im having a problem and i dont see where i made error. Im tryting to write something into XML file. Also how do you read textbox input and save it into XML ??

Cheers for help :)

public class CourseName
        {
            string _name;
            public CourseName(string name)
            {
                this._name = name;
            }            
            public string Name { get { return _name; } }            
        }


        private void button1_Click(object sender, EventArgs e)
        {
            CourseName[] courseNames = new CourseName[4];
            courseNames[0] = new CourseName("blablabla");
            using (XmlWriter writer = XmlWriter.Create("myCourses1234.xml"))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("myCourse");
                foreach (CourseName coursename in courseNames)
                {
                    writer.WriteStartElement("myCourse");
                    writer.WriteElementString("courseName", coursename.Name); //<---- Object reference not set to an instance of an object
                    writer.WriteEndElement();

                }
                writer.WriteEndElement();
                writer.WriteEndDocument();


            }
        }

Line 14 you create an array of CourseName with a size of 4.
Line 20 you iterate through all these CourseName objects (foreach on an array gives all array elements, set or not). So since you only set one of the array elements (in line 15), indexes 1, 2, and 3 are all null, thus there is no courseName.Name and the error.

Use a List<T> or set all the elements of the array.

I agree with Momerath on this one, If you want to keep using arrays instead of a List as Momerath suggests I suggest you initialize all the values first. something like :

for (int i = 0; i<courseNames.length();i++)
 {
  courseNames[i] = new CourseName("");
 }
 //This will make empty string values but now you have used the "new" keyword to initiallize. 

OK cheers guys, i have changed it now

            CourseName[] courseNames = new CourseName[3];
            courseNames[0] = new CourseName("blablabla");
            courseNames[1] = new CourseName("ugabuga");
            courseNames[2] = new CourseName("blauga");

but when i press button nothing happens ( at least i dont get error ) but XML file is not created and saved...

Still not working ;/.
PS. textboxes are irrelevant atm as i will later get data from it to store in xml. but for now im trying just to create xml file when button is clicked.

private void button1_Click(object sender, EventArgs e)
        {
            //initialize text box
            string textboxnumber1;
            string textboxnumber2;
            string textboxnumber3;
            string assesmentbox1;
            string assesmentbox2;
            string assesmentbox3;
            string assesmentbox4;

            //get values from text box
            textboxnumber1 = textBox1.Text;
            textboxnumber2 = textBox2.Text;
            textboxnumber3 = textBox3.Text;


            CourseName[] courseNames = new CourseName[3];
            courseNames[0] = new CourseName("blablabla");
            courseNames[1] = new CourseName("ugabuga");
            courseNames[2] = new CourseName("blauga");

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");
            using (XmlWriter writer = XmlWriter.Create("myCourses1234.xml", settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("myCourse");
                foreach (CourseName coursename in courseNames)
                {
                    writer.WriteStartElement("myCourse");
                    writer.WriteElementString("courseName", coursename.Name);
                    writer.WriteEndElement();
                    writer.Flush();

                }
                writer.WriteEndElement();
                writer.WriteEndDocument();


            }
        }

Try calling flush after WriteEndDocument. I think it should still be writing everything up until that point though...

This article has been dead for over six months. Start a new discussion instead.