如何将列表框中的数据及其文本框中的相应值保存到xml中 [英] How to save data from a listbox and its corresponding value in a textbox into an xml

查看:82
本文介绍了如何将列表框中的数据及其文本框中的相应值保存到xml中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表框中有一些项目,当我单击特定项目时,我需要在文本框中输入相应的值,而这些必须保存在xml中.例如.我的列表框包含姓名和年龄.我单击名称,在文本框中输入相应的值(例如John),我需要将其保存到xml中.必须对列表框中的所有项目执行此操作.我是一个初学者,不太确定如何去做.我希望我的问题是可以理解的.我该怎么办?谢谢!

I have some items in a listbox, and when i click on the particular item, i need to enter the corresponding value in a text box, and these have to be saved in an xml. For example. my listbox contains name and age. I click on name, enter the corresponding value(say John) in the text box and i need to save this to an xml. This has to be done for all the items in the listbox. I am a beginner and not too sure how to go about it. I hope my question is comprehensbile. How can i go about this? Thanks!

推荐答案

首先需要确定的几件事:

1-您要编写的xml文件的结构,可能是这样的:

There are few things you need to decide first:

1- Structure of xml file that you want to write, it may be something like this:

<people>
<name>John</name>
<age>26</age>
</people>
<people>
<name>xyz</name>
<age>28</age>
.
.
</people>



或者您可以提出其他一些选择

2-当需要将数据写入xml文件时,在捕获一组数据之后立即或仅在捕获所有数据集结束时.

之后,您可以使用以下链接将数据写入xml文件:

http://forum.codecall.net/csharp-tutorials/31315-c-tutorial-reading-writing-xml-files.html

希望对您有帮助...



Or you can come up with something else

2- When the data needs to be written in xml file, immediately after one set of data is captured or only at the end when all data sets are captured.

After this you can use following link to write data into xml file:

http://forum.codecall.net/csharp-tutorials/31315-c-tutorial-reading-writing-xml-files.html

Hope it helps...


我现在有了这个:

i have this now:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create("Query_advanced.xml", settings);
            writer.WriteStartDocument();
            writer.WriteStartElement("Query");
            foreach (string item in listBox1.SelectedItems)
            {
                               
                    writer.WriteStartElement("Attribute");
                    writer.WriteAttributeString("Name", item);
                    //writer.WriteAttributeString("Name", "Patient Name");
                    writer.WriteAttributeString("Value", textBox1.Text);
                    writer.WriteEndElement();
                
                //   catch
                //{
                //    throw;
                //}
                
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
            
        }



而且我需要在退出循环之前保存项目,然后为此每次都更新xml.我该怎么办?



And i need to save the items before coming out of the loop, and then update the xml everytime for this. How can i go about?


这篇关于如何将列表框中的数据及其文本框中的相应值保存到xml中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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