使用类C#将其写入XML [英] Writing it to XML using classes C#

查看:52
本文介绍了使用类C#将其写入XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从GUI屏幕读取数据.例如,其中包含1.名称2.子名称3.最喜欢的数字.

名称",子名称"和最喜欢的数字是文本框.我将通过将详细信息附加到xml文档来创建更多名称.是否存在任何类实现,用于在写入XML之前存储来自GUI的值.通过使用SubName类,例如

I need to read data from the GUI screen. Which has for example, 1. Name 2. Sub-Name 3. Favorite Numbers.

The "Name", "Sub-Name" and "Favorite Numbers are text boxes.I would create more Names by appending the details to the xml document. is there any class implementation for storing the values from GUI before writing to the XML. In my case by using the class SubName like

Class SubName
{
public SubName
{
Num = new List<int>();
}
public string Sub-Name{get;set;}
public list<int> Num{get;set;}
}
</int></int>



我试图将"Num"值存储在列表中.我需要验证所有准备好写入XML的子名称,因为某些规则限制了所有子名称中存在的"Num"值的总和.

因此,通过拥有



i tried to store the "Num" values in the List. I need to validate all of the Sub-Names to be made ready to write to XML, as there is certain rule that limits the total sum of "Num" values present in all of Sub-Names.

so, by having

<pre lang="c#">SubName[] subs = new SubName[10]



并通过
将我从GUI屏幕读取的值分配给类值



and assign the values that i read from the GUI screen to the class values by,

subs[0] = new SubName();
subs[0].Sub-Name = txtboxSubName.Text
subs[0].Num = listNum



其中,listNum的类型为



where, listNum is of type

<pre lang="c#">List<int></int>

.

我的问题是在创建XML标记时,正确编写了Sub-Name,但是不存在Num值.有什么解决办法.否则有没有比这更好的方法了.预期的XML标签显示在下面...

.

My problem is while creating the XML tags, the Sub-Name is properly written but the Num values are not present. Is there any solution. Or else is there any better method than this. The expected XML tags are presented below...

<Header>
<names>
<name>TEST</name><sub>
<sub-name>XYZ</sub-name>
<num>1</num><num>2</num><num>3</num><num>4</num><num>5</num><num>6</num><num>7</num>
<sub-name>XYZ</sub-name>
<num>1</num><num>2</num><num>3</num><num>4</num><num>5</num><num>6</num><num>7</num>
<sub-name>XYZ</sub-name>
<num>1</num><num>2</num><num>3</num><num>4</num><num>5</num><num>6</num><num>7</num>
<sub-name>XYZ</sub-name>
<num>1</num><num>2</num><num>3</num><num>4</num><num>5</num><num>6</num><num>7</num>
</sub></names>
<names>
<name>ROTW</name><sub>
<sub-name>XYZ</sub-name>
<num>1</num><num>2</num><num>3</num><num>4</num><num>5</num><num>6</num><num>7</num>
<sub-name>XYZ</sub-name>
<num>1</num><num>2</num><num>3</num><num>4</num><num>5</num><num>6</num><num>7</num>
<sub-name>XYZ</sub-name>
<num>1</num><num>2</num><num>3</num><num>4</num><num>5</num><num>6</num><num>7</num>
<sub-name>XYZ</sub-name>
<num>1</num><num>2</num><num>3</num><num>4</num><num>5</num><num>6</num><num>7</num>
</sub></names></Header>

推荐答案

您是否要临时使用XML?你为什么需要它?最好使用数据合同System.Runtime.Serialization.DataContractSerializer.它可以帮助您存储和加载(还原)任何数据,而无需手动使用XML.您将获得一个功能强大的工具,该工具具有以下功能:拥有世界唯一的架构,持久存储任意结构的任意对象集(不仅是树)以及在迁移架构时备份兼容的方法-无需您付出任何努力.

参见:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractserializer.aspx [ ^ ].

另请参阅我过去提倡使用此方法的解决方案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [
Are you going to use XML ad-hoc? Why would you need it? Better use Data Contract and System.Runtime.Serialization.DataContractSerializer. It will help you to store and load (restore) any data without manual work with XML. You will get a powerful tool with capabilities of having world-unique schema, persisting arbitrary objects sets of arbitrary structure (not only trees) and means of backup compatibility while migrating the schema — all without your effort.

See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^].

See also my past solutions where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating a property files...[^].

—SA


这篇关于使用类C#将其写入XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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