简体   繁体   English

XML,数据表和变量-初学者问题

[英]Xml, datatable, and variables - beginner questions

I have a project that has to export variables and fixed values into a xml file. 我有一个项目,必须将变量和固定值导出到xml文件中。 The idea is to save the work done, and load it again later. 这个想法是保存所做的工作,并在以后再次加载。

Please be aware i just beginned to study c#, so shortened explanations will be hard to understand (i've been reading about other issues as well) 请注意,我刚刚开始学习C#,因此简短的解释将很难理解(我也一直在阅读其他问题)

In some links i've read that such this is easy when you create datatables, but on the examples i've read they only talk about fixed values, and as far as i've seen, they required the table to be created in the form (wich is not the objective). 在某些链接中,我读到在创建数据表时,这很容易,但是在我阅读的示例中,它们仅讨论固定值,据我所知,他们要求在表中创建表。形式(不是目标)。

I've tried to place a code i've found around 我试图放置一个我发现的代码

DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));


table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

It's pretty neat, but, can i visualy see the table somewhere? 这很整洁,但是,我可以在某处看到桌子吗? or it's like a console list? 还是控制台列表?

Can i use variables, and will it update the matrix when the program changes the variable values? 我可以使用变量吗,当程序更改变量值时,它会更新矩阵吗?

Will the table be acessible in all the forms i create in the project? 在我在项目中创建的所有表格中,表格都可以使用吗? Or should i create her in a separate class and make it public? 还是应该在单独的班级中创建她并将其公开?

How recommended is this process when the main objective is export / import data trough xml files? 当主要目标是通过xml文件导出/导入数据时,如何推荐此过程?

edit: i will also have to add information about checkboxes, dropdown lists, etc, not only variables, so i'm wondering if datatables are really the best way to export/import xml. 编辑:我还必须添加有关复选框,下拉列表等的信息,而不仅仅是变量,所以我想知道数据表是否真的是导出/导入xml的最佳方法。

You can convert a DataTable into XML Like this: 您可以像这样将DataTable转换为XML:

string fileName =myAppPath + @"\sample.xml";

DataTable table = new DataTable();
... //your code goes here...
table.WriteXml(fileName,  XmlWriteMode.WriteSchema);

And read it like this: 并这样阅读:

string fileName =myAppPath + @"\sample.xml";
DataTable newTable = new DataTable();
newTable.ReadXml(fileName);

Regarding: 关于:

Can I use variables, and will it update the matrix when the program changes the variable values? 我可以使用变量吗,当程序更改变量值时,它会更新矩阵吗?

As for variables I guess your mean memory. 至于变量,我猜你的平均记忆。 And the Your DataTable is a variable and it will hold everything in memory until your write it to the file. 而且,您的数据表是一个变量,它将所有内容保存在内存中,直到您将其写入文件为止。 If on the other hand you mean binding the values on the table to a control, well that would depend on the type of of technologies you are using. 另一方面,如果您的意思是将表上的值绑定到控件,那么这将取决于您使用的技术类型。

Will the table be accessible in all the forms I create in the project? 我可以在项目中创建的所有表格中访问该表吗?

Variables only exists within an scope and by scope to simplify I mean any two curly brackets "{}". 变量仅存在于一个范围内,为简化起见,我的意思是任何两个大括号“ {}”。 In web applications(ASP.NET) for example you can use something called "session' to share data across forms and of course there are way to work this arounds in most technologies but your post does not specify it. 例如,在Web应用程序(ASP.NET)中,您可以使用一种称为“会话”的东西来跨表格共享数据,当然,在大多数技术中,都有解决此问题的方法,但是您的文章未指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM