如何写/读/修改数组到XML文件? [英] How to write/ read/ modify array to XML file?

查看:68
本文介绍了如何写/读/修改数组到XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,其结构是:

I have an array which its structure is:

<data>
<id></id>
<list></list>
</data>

我想写一个数组来列出节点

And i want to write an array to list node

<data>
<id></id>
<list>
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
</list>
</data>

如何编写/读取/修改xml数组中的数组?

How to write / read/ modify an array which inside the xml array?

有什么想法吗?


推荐答案

其中一种解决方案可以基于 XDocument .检查示例,其中添加了一些项目:

One of the solutions can be based on XDocument. Check an example, which adds some items:

string sample = @"<data><id></id><list></list></data>";

XDocument doc = XDocument.Parse( sample );

Console.WriteLine("--- Initial XML:");
Console.WriteLine(doc.ToString(SaveOptions.None));


var list = doc.Root.Element( "list" );

// add some items
for(int i = 1; i <= 4; ++i )
{
	string sample_value = i.ToString();
	list.Add( new XElement( "item", sample_value ) );
}


Console.WriteLine("--- Result:");
Console.WriteLine(doc.ToString(SaveOptions.None));

为简单起见,它从字符串中加载XML.为了处理文件,请使用 XDocument.Load Save .

For simplicity, it loads XML from string. In order to deals with files, use XDocument.Load and Save.


这篇关于如何写/读/修改数组到XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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