Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Someone,
I don't have "System.Xml.XmlDocument " or System.Xml.XmlReader" in my using system possibilities. Is there something I should do to add them please? And How should I do this? I've downloaded the free version of Visual Studio free Community 2015. The only thing that are in it are "using System.Xml" and "using System.Linq". But I can't work with Linq. It's new to me. Also, it's been 5 years that I haven't code anymore so I'm very rusted.
Nevertheless, I wanted to add the content of a selected XML file in a textbox on a second form so the user can edit this file and save it afterwards.
Is there somebody that could help me please? Because the only thing that happens is the user can navigate to a folder on his harddrive, select the folder with all the XML files. He then has an overview of all files in the first textbox and can select one file to open where a second form's opening but without the textbox!? So actually I'm missing my second textbox and the xml-content.

Thanks in advance.

What I have tried:

public partial class Form1 : Form
    {
        //get files from directory
        string[] path = Directory.GetFiles(@"C:\Users\decraiec\Documents\Atrias_Automated", "*.XML");


        public Form1()
        {
            InitializeComponent();
            listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
        }

        private void btnGetFiles_Click(object sender, EventArgs e)
        {
            //read all files in path
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                //if there is text in the listbox clear it first
                listBox1.Items.Clear();
                //create an array for the files and the directory.
                string[] files = Directory.GetFiles(fbd.SelectedPath);
                string[] dirs = Directory.GetDirectories(fbd.SelectedPath);
                //show every file in the listbox
                foreach (string file in files)
                {
                    listBox1.Items.Add(file);
                }
                foreach (string dir in dirs)
                {
                    listBox1.Items.Add(dir);
                }
            }
        }

        public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            string currentItem = listBox1.SelectedItem.ToString();
            if (listBox1.SelectedIndex == -1)
            {
                MessageBox.Show("Item is not available in Listbox");
            }
            else
            {

                //read the file and give it back in the second form in the listbox
                var xmlString = File.ReadAllText(currentItem.ToString());
                var stringReader = new StringReader(xmlString);
                var dsSet = new DataSet();
                dsSet.ReadXml(stringReader);

                //show second form with listbox on it and the clicked xml file content
                frmEditXML frm2 = new frmEditXML();
                TextBox txb2 = new TextBox();
                txb2.Text = dsSet.GetXml();
                txb2.Text = dsSet.GetXmlSchema();
                frm2.ShowDialog();
                txb2.Show();

            }
        }



        private void Form1_Load(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }

C#
public partial class frmEditXML : Form
   {


       private void frmEditXML_Load(object sender, EventArgs e)
       {
              //open textfile and show content that was selected
       }



       private void btnSaveChanges_Click(object sender, EventArgs e)
       {
           //Text from the richtextbox
           string strsave = tbxSaveChanges.Text;
           //Create a new SaveFileDialog object
           using (SaveFileDialog sfdSave = new SaveFileDialog())
               try
               {
                   //Available file extension
                   sfdSave.Filter = "XML (*.xml*)|*.xml*";
                   //Show SaveFileDialog
                   if (sfdSave.ShowDialog() == DialogResult.OK && sfdSave.FileName.Length > 0)
                   {
                       //save file as xml
                       UTF8Encoding utf8 = new UTF8Encoding();
                       StreamWriter sw = new System.IO.StreamWriter(sfdSave.FileName, false, utf8);
                       sw.Write(strsave + " - NEG");
                       sw.Close();
                   }
               }
               catch (Exception errorMsg)
               {
                   MessageBox.Show(errorMsg.Message);
               }
           Application.Exit();
       }

       private void frmEditXML_FormClosed(object sender, FormClosedEventArgs e)
       {
           Application.Exit();
       }

   }
Posted
Updated 14-Feb-17 0:51am
Comments
ZurdoDev 13-Feb-17 7:52am    
Right click your Project and add a reference to System.Xml. Then they will show up in your usings.
AAB40 14-Feb-17 6:35am    
Hi RyanDev, thanks but I've tried that already and the list of references that we can add is very long without specific other 'xml' additives. the only one that's in there is the one I already have: "using System.Xml;". But nevertheless, if I follow the way of thinking of Richard, I won't need this anymore (I think). Thanks for answering.

You could simplify things by just having an Edit control on your main form that allows the user to do the editing. And instead of the ListBox to select a file use the OpenFileDialog Class (System.Windows.Forms)[^].

Also if you want to use a XmlDocument Class (System.Xml)[^] then you just need System.Xml in your reference set.
 
Share this answer
 
Comments
AAB40 13-Feb-17 11:42am    
Thank you Richard for this quick answer. But how do you suggest to do this? As the user needs to select a particular file out of the list before editing it? that are 2 actions instead of 1. Select de file with the cursor and click the 'Edit' button. The "btnGetFiles_Click" only shows a list of all files retrieved from the main folder....
Richard MacCutchan 13-Feb-17 11:58am    
You can set the options in the OpenFileDialog to direct the user to the correct folder and only select XML files. It's all in the documentation.
AAB40 14-Feb-17 6:33am    
thanks Richard, that was the easy part, using OpenFileDialog. But it's just writing the title that the user is selecting in the richTextBox, not showing the XML content. Therefore I need something like a reader for the xml file. So first, where should I put the code? within the if-loop or outside? secondly, I don't have specific nodes that I want to change for the moment, it's just reading the XML file and show it completely in the textbox.
AAB40 14-Feb-17 6:45am    
Richard, I've found the way. Thanks for your time.
Work arround as Richard MacCutchan suggested: I have used 1 button to get the file and 1 button to save the changes.

public partial class frmEditXML : Form
{
    //get files from directory
    string[] path = Directory.GetFiles(@"C:\Users\decraiec\Documents\Atrias_Automated", "*.XML");


    public frmEditXML()
    {
        InitializeComponent();

    }



    private void btnEditfile_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            richTextBox1.Text = openFileDialog1.FileName;
            File.ReadAllText(richTextBox1.Text);
        }
        openFileDialog1.Filter = "xml files|*.xml|All files|*.*";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
            }
            catch (Exception exc)
            {
                MessageBox.Show("An error occured: " + System.Environment.NewLine + exc.ToString() + System.Environment.NewLine);
                throw;
            }
        }

    }

    private void frmOpenRead_Load(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void btnSavechanges_Click(object sender, EventArgs e)
    {
        if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
        }
    }
}
 
Share this answer
 
Comments
AAB40 2-Mar-17 5:35am    
Why was this one deleted please? I had a problem, someone helped med and I found a solution.....! ???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900