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.

provide an exception handler before the condition

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

You never flush the buffer. See here for an example

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.