如何在第二个表单的文本框中检索读取XML文件的内容? [英] How do I retrieve the content of a read XML file in a textbox on a second form?

查看:65
本文介绍了如何在第二个表单的文本框中检索读取XML文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人,

我在使用系统的可能性中没有System.Xml.XmlDocument或System.Xml.XmlReader。请问我应该做些什么来添加它们?我应该怎么做?我已经下载了免费版的Visual Studio免费社区2015.其中唯一的东西是使用System.Xml和使用System.Linq。但是我无法使用Linq。这对我来说是新的。而且,我已经5年没有编码了,所以我非常生锈。

尽管如此,我想在一个选定的XML文件中添加内容第二个表单上的文本框,以便用户可以编辑此文件并在之后保存。

是否有人可以帮助我?因为唯一可能发生的事情是用户可以导航到他的文件夹harddrive,选择包含所有XML文件的文件夹。然后他会对第一个文本框中的所有文件进行概述,并且可以选择一个文件打开第二个表单的打开但没有文本框!?所以实际上我是错的唱我的第二个文本框和xml内容。



提前致谢。



什么我试过了:



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();
        }
    }




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();
       }

   }

推荐答案

你可以简化一下编辑主窗体上的控件,允许用户进行编辑。而不是ListBox来选择文件使用 OpenFileDialog类(System.Windows.Forms) [ ^ ]。



如果你想使用 XmlDocument类(System.Xml) [ ^ ]然后你就是需要在参考集中使用System.Xml。
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.


在Richard MacCutchan建议的工作环境中:我使用1按钮获取文件,使用1按钮保存更改。



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);
        }
    }
}


这篇关于如何在第二个表单的文本框中检索读取XML文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆