How do I load xml used in form1 in form2?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: How do I load xml used in form1 in form2?

  1. #1
    Join Date
    Apr 2011
    Posts
    4

    Angry How do I load xml used in form1 in form2?

    the program that I am writing has a listbox that displays application names (read from an xml). when I click on an application name in the listbox it runs the application.

    by the way, I am using visual studio professional 2010 4.0

    What I want to add is this. I want to be able to drag and drop an application into the listbox, have a second form come up with text boxes for the user to enter the application name (as they want it to appear in the list) and the application path. After clicking "submit" on form2 it will write to the existing xml, close form2, and refresh the listbox in form1.

    I am able to get the form2 to come up as soon as an application icon is dropped in the listbox.
    Here are my issues:
    1. form2 won't load the xml. I get an error that states "the name w7_apps.xml does not exist in the current context" w7_apps.xml is obviously the name of my xml file.
    2. since I can't expect the user to always know the path, instead I'd like to have the path automatically added to the xml when the application icon is dropped in the listbox.


    any help is appreciated. Thanks! my code from form2 is below. let me know if you need more info. This is my first time posting in this forum.

    private void btnSubmit_Click(object sender, EventArgs e)
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(w7_apps.xml); //this is where I get the error

    if (txtDesc.Text != null)
    {
    XmlNode descNode = doc.CreateNode(XmlNodeType.Element, "app_desc", "");
    descNode.InnerText = txtDesc.Text;
    doc.DocumentElement.AppendChild(descNode);
    }
    else
    {
    MessageBox.Show("You must enter a description",
    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    if (txtPath.Text != null)
    {
    XmlNode pathNode = doc.CreateNode(XmlNodeType.Element, "app_path", "");
    pathNode.InnerText = txtPath.Text;
    doc.DocumentElement.AppendChild(pathNode);
    }
    else
    {
    MessageBox.Show("You must enter a path" + "\n" +
    "Right click on application icon" + "\n" +
    "Click properties, then copy path and paste in textbox",
    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }
    Last edited by Bluzman; May 6th, 2011 at 08:26 PM.

  2. #2
    Join Date
    Oct 2006
    Posts
    82

    Re: How do I load xml used in form1 in form2?

    I didn't go into your code too deeply, but I have this come up on occasion when my mind wanders. Assuming that w7_apps.xml is a reference variable to some file info and that it was defined in the form1 code, it will not be defined in the form2 code. Recall that the two forms are in different classes and what is deined in one is not magically accessible to the other despite them being in the same namespace.

    There are several possible solutions:
    1) Declare the variable "public", and then reference it using Form.Parent() (if I recall correctly).
    2) Pass the variable as a parameter in the call to open form2. Yes, you *can* do this! I do this often. In the form2 constructor, store the variable in a class variable.

    Of course I may have misunderstood your problem.

  3. #3
    Join Date
    Apr 2011
    Posts
    4

    Re: How do I load xml used in form1 in form2?

    Thanks for the reply. Still having issues with it, even when declaring the xml string as public. Might try to figure another way to do this.

  4. #4
    Join Date
    Oct 2006
    Posts
    82

    Re: How do I load xml used in form1 in form2?

    Quote Originally Posted by Bluzman View Post
    Thanks for the reply. Still having issues with it, even when declaring the xml string as public. Might try to figure another way to do this.
    When you decide on an way. please post your solution and why you chose it. I still have trouble with this and so its likley others have also. For anyone who knows, please post a suggestion.

  5. #5
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: How do I load xml used in form1 in form2?

    Not sure if this is the case, but if this is your code in your form....

    Code:
    doc.Load(w7_apps.xml);
    Then it should probably be...

    Code:
    doc.Load("w7_apps.xml");
    Without the Quotes, I would think that the doc.Load() function would assume that w7_apps.xml is a namespace/class called w7_apps, and a class/property/field called XML

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center