将多个值从单个文本框保存并返回到单个XML文件。 [英] Save and Return multiple values from a single text box to single XML file.

查看:64
本文介绍了将多个值从单个文本框保存并返回到单个XML文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个小项目中工作,而不是所有编程方面的专家。



有一定要求我必须从单个文本中保存条目框到一个XML文件,并在需要时将它们返回到另一个多行文本框。

一行中的每个条目。

IE

toolstriptextbox_1条目1 = abc

条目2 = xyz



等..



我的代码(下面)设法将条目保存到XML文件,但它会复制相同的第一个条目,而不是每次文本框中的新条目更改时捕获不同的条目。



这是代码的一部分。涉及此操作。



\\保存部分\\创建它作为一个单独的类。



命名空间HW_Browser

{

公共类SaveXml

{

public static void Saveurl(object myurl,string filename)

{

XmlSerializer sr = new XmlSerializer(myurl.GetType());

TextWriter writer = new StreamWriter(filename );

sr.Serialize(作家,myurl);

writer.Close();



}



}

}

+++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++这确实是这样,但我知道\\it的单独类也处理XML文件的读写。



  namespace  HW_Browser 
{
public class browsinghistory
{
private string entry1;
private string entry2;
private string entry3;
private string entry4;

public string Entry1
{
get { return entry1; }
set {entry1 = value ; }
}
public string Entry2
{
get { return entry2; }
set {entry2 = value ; }
}
public string Entry3
{
get { return entry3; }
set {entry3 = value ; }
}
public string Entry4
{
get { return entry4; }
set {entry4 = value ; }
}

}
}

+++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ code-keyword> 其中它被保存到XML文件\\由按钮触发单击
尝试
{



browhistory his = new 浏览记录();
his.Entry1 = toolStripTextBox1.Text;
his.Entry2 = toolStripTextBox1.Text;
his.Entry3 = toolStripTextBox1.Text;
his.Entry4 = toolStripTextBox1.Text;
SaveXml.Saveurl(他的, history.xml);


}
catch (Exception ex)
{
MessageBox.Show(ex 。信息); b

$ b +++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -keyword> 其中​​ XML文件应该将显示回另一个多行文本框。

if (File.Exists( history.xml))
{
XmlSerializer xs = new XmlSerializer( typeof运算(browsinghistory));
FileStream read = new FileStream( history .xml,FileMode.Open,FileAccess.Read,FileShare.Read);
browhistory his =(browsinghistory)xs.Deserialize(read);
textBox1.Text = his.Entry1;
textBox1.Text = his.Entry2;
textBox1.Text = his.Entry3;
textBox1.Text = his.Entry4;
read.Close();
}

解决方案

获取文本框的行如下



  if (toolStripTextBox1.Lines.Length> 0)
his.Entry1 = toolStripTextBox1.Lines [ 0 ];
if (toolStripTextBox1.Lines.Length> 1)
his.Entry2 = toolStripTextBox1.Lines [ 1 ];
if (toolStripTextBox1.Lines.Length> 2)
his.Entry3 = toolStripTextBox1.Lines [ 2 ];
if (toolStripTextBox1.Lines.Length> 3)
his.Entry4 = toolStripTextBox1.Lines [ 3 ];





设置如下行

 toolStripTextBox1.Text = his.Entry1 + Environment.NewLine 
+ his.Entry2 + Environment.NewLine
+ his.Entry3 + Environment.NewLine
+ his.Entry4 + Environment.NewLine;


I am working in a small project , not at all expert in programming.

There is a certain requirement where in I have to save entries from a single text box to an XML file and return them back when required to a different Multi-line text box.
Each entry in a line.
IE
toolstriptextbox_1 Entry 1 = abc
Entry 2 = xyz

etc..

My code (below) managed to save entries to the XML file, however it duplicates the same first entry rather than capturing different entries each time a new entry in the text box changes.

Here is the portion of the code. that involves this operation.

\\ The saving parts\\ created it as a separate class.

namespace HW_Browser
{
public class SaveXml
{
public static void Saveurl(object myurl, string filename)
{
XmlSerializer sr = new XmlSerializer(myurl.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, myurl);
writer.Close();

}

}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++
\\Not sure what this exactly does but I know \\it's separate class as well deals with the XML file reading and writing.

namespace HW_Browser
{
   public class browsinghistory
    {
        private string entry1;
        private string entry2;
        private string entry3;
        private string entry4;

        public string Entry1
        {
            get { return entry1; }
            set { entry1 = value; }
        }
        public string Entry2
        {
            get { return entry2; }
            set { entry2 = value; }
        }
        public string Entry3
        {
            get { return entry3; }
            set { entry3 = value; }
        }
        public string Entry4
        {
            get { return entry4; }
            set { entry4 = value; }
        }

    }
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\\here is where it gets saved to the XML file \\ triggered by a button click 
try
            {
                
                

                browsinghistory his = new browsinghistory();
                his.Entry1 = toolStripTextBox1.Text;
                his.Entry2 = toolStripTextBox1.Text;
                his.Entry3 = toolStripTextBox1.Text;
                his.Entry4 = toolStripTextBox1.Text;
                SaveXml.Saveurl(his, "history.xml");

  
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

\\ This is where the XML file supposed to displays the value back to the other multi-line text box.

if (File.Exists("history.xml"))
            {
                XmlSerializer xs = new XmlSerializer(typeof(browsinghistory));
                FileStream read = new FileStream("history.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
                browsinghistory his = (browsinghistory)xs.Deserialize(read);
                textBox1.Text = his.Entry1;
                textBox1.Text = his.Entry2;
                textBox1.Text = his.Entry3;
                textBox1.Text = his.Entry4;
                read.Close();
            }

解决方案

get lines of the text box as below

if(toolStripTextBox1.Lines.Length >0)
  his.Entry1 = toolStripTextBox1.Lines[0];
if(toolStripTextBox1.Lines.Length >1)
   his.Entry2 = toolStripTextBox1.Lines[1];
if(toolStripTextBox1.Lines.Length >2)
  his.Entry3 = toolStripTextBox1.Lines[2];
if(toolStripTextBox1.Lines.Length >3)
  his.Entry4 = toolStripTextBox1.Lines[3];



set lines as below

toolStripTextBox1.Text = his.Entry1 + Environment.NewLine 
                         + his.Entry2 + Environment.NewLine 
                         + his.Entry3 + Environment.NewLine 
                         + his.Entry4 + Environment.NewLine;


这篇关于将多个值从单个文本框保存并返回到单个XML文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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