Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:09:22+00:00 2026-05-31T11:09:22+00:00

I have a function, InsertItems: public void InsertItems() { todoitemList.Clear(); todoSelect.Items.Clear(); foreach (XmlNode xmlNode

  • 0

I have a function, InsertItems:

    public void InsertItems()
    {
        todoitemList.Clear();
        todoSelect.Items.Clear();

        foreach (XmlNode xmlNode in xmlDoc.SelectNodes("ToDoList/ToDo"))
        {
            ToDoItem item = new ToDoItem();

            item.ID = xmlNode.SelectSingleNode("ID").InnerText;
            item.Title = xmlNode.SelectSingleNode("Title").InnerText;
            item.Description = xmlNode.SelectSingleNode("Desc").InnerText;
            item.PriorityLevel = xmlNode.SelectSingleNode("Priority").InnerText;
            item.Date = Convert.ToDateTime(xmlNode.SelectSingleNode("Date").InnerText);
            item.TimeHour = Convert.ToInt32(xmlNode.SelectSingleNode("TimeHour").InnerText);
            item.TimeMinute = Convert.ToInt32(xmlNode.SelectSingleNode("TimeMinute").InnerText);
            item.TimeSecond = Convert.ToInt32(xmlNode.SelectSingleNode("TimeSecond").InnerText);
            item.Completed = xmlNode.SelectSingleNode("Completed").InnerText;

            todoitemList.Add(item);
            todoSelect.Items.Add(item.Title);
            todoIDList.Add(item.ID);
        }
    }

This function clears both a list and the combo box to which is used to select the items and then fills the list with the relevant data. ToDoItem is a class which contains properties – ID, Title and so forth.

When the function is executed within Form1.cs, it fully works as expected, clearing the list and adding the new data. However, when the function is executed within Form2.cs (main.InsertItems()), the foreach loop appears to be never ran and I’ve really no idea what is causing this.

Any help is much appreciated!

—

Edit:

main.InsertItems() is called in the following function:

    private void createNew_Click(object sender, EventArgs e)
    {
        if (CheckAll())
        {
            XmlNode xmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "ToDo", null);

            XmlNode xmlNodeID = xmlDoc.CreateElement("ID");
            xmlNodeID.InnerText = CreateRandomID();

            XmlNode xmlNodeTitle = xmlDoc.CreateElement("Title");
            xmlNodeTitle.InnerText = textBoxTitle.Text;

            XmlNode xmlNodeDesc = xmlDoc.CreateElement("Desc");
            xmlNodeDesc.InnerText = textBoxDesc.Text;

            XmlNode xmlNodePriority = xmlDoc.CreateElement("Priority");
            xmlNodePriority.InnerText = Convert.ToString(priorityLevel.SelectedItem);

            XmlNode xmlNodeDate = xmlDoc.CreateElement("Date");
            string currentDate = Convert.ToString(monthCalendar.SelectionRange.Start);
            string strippedDate = currentDate.Substring(0, currentDate.Length - 8);
            strippedDate += timeHour.Text + ":" + timeMinute.Text + ":" + timeSecond.Text;
            xmlNodeDate.InnerText = strippedDate;

            XmlNode xmlNodeTimeHour = xmlDoc.CreateElement("TimeHour");
            xmlNodeTimeHour.InnerText = timeHour.Text;

            XmlNode xmlNodeTimeMinute = xmlDoc.CreateElement("TimeMinute");
            xmlNodeTimeMinute.InnerText = timeMinute.Text;

            XmlNode xmlNodeTimeSecond = xmlDoc.CreateElement("TimeSecond");
            xmlNodeTimeSecond.InnerText = timeSecond.Text;

            XmlNode xmlNodeCompleted = xmlDoc.CreateElement("Completed");
            xmlNodeCompleted.InnerText = "False";

            xmlNode.AppendChild(xmlNodeID);
            xmlNode.AppendChild(xmlNodeTitle);
            xmlNode.AppendChild(xmlNodeDesc);
            xmlNode.AppendChild(xmlNodePriority);
            xmlNode.AppendChild(xmlNodeDate);
            xmlNode.AppendChild(xmlNodeTimeHour);
            xmlNode.AppendChild(xmlNodeTimeMinute);
            xmlNode.AppendChild(xmlNodeTimeSecond);
            xmlNode.AppendChild(xmlNodeCompleted);

            xmlDoc.DocumentElement.AppendChild(xmlNode);

            try
            {
                xmlDoc.Save(_fileName);
                MessageBox.Show("Item successfully added!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                main.InsertItems();
            }
            catch (XmlException)
            {
                MessageBox.Show("Error! The item could not be added due to an XML error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (IOException)
            {
                MessageBox.Show("Error! The file could not be found or written to. Item could not be added.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception)
            {
                MessageBox.Show("Error! An unknown error occured. Item could not be added.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.Close();
        }
    }

CheckAll returns a boolean -> true if all fields are valid.

_fileName variable is correct and is saving to the correct file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:09:23+00:00Added an answer on May 31, 2026 at 11:09 am

    The problem is that you’re not reloading xmlDoc in Form1.InsertItems.

    In the code you posted, before calling Form1.InsertItems from Form2, you write the file to disk, but since you’re not passing a reference to the updated xmlDoc from Form2 on to Form1, you don’t see the changes show up on Form1. InsertItems works as expected when you call it from Form1 because xmlDoc is a member variable for Form1, so the changes are available in InsertItems when called from Form1.

    Try either reloading the XmlDocument from the file system at the beginning of InsertItems or pass an XmlDocument to InsertItems as a parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have function named: - (void) AddSortedCustomFeed :(NSMutableArray*) rssList; this function adds items(Articles) from
I have function along these lines: public void view(string msg) { messagebox.show(msg); } .
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have function CREATE OR REPLACE FUNCTION public.GetSoilCod ( cod1 integer = 0, cod2
Ok my problem: I have function to create a Label: - (void)crateBall:(NSInteger *)nummer {
I have function whose signature is something like so: void someFunc(ifstream ifile) { This
I have function as below . public static object getClassInstance(string key, object constructorParameter) {
I have function in C++ that takes a vector and pushes some items onto
I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,
I have function getCartItems in cart.js and I want to call that function in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.