没有合适的资源?快使用搜索试试~ 我知道了~
首页C# 读写XML文件实例代码
资源详情
资源评论
资源推荐

C# 读写读写XML文件实例代码文件实例代码
在本篇文章里小编给大家整理的是关于C# 读写XML文件最简单方法,需要的朋友们可以跟着学习参考下。
C#史上最简单读写xml文件方式,创建控制台应用程序赋值代码,就可以运行,需要改动,请自行调整
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace ConsoleApp1
{
class Program
{
public const String xmlPath = "info.xml";
static void Main(string[] args)
{
IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();
infos.Add("Evan", new List<string>() { "123", "456" });
SaveXML(infos);
ReadXML();
Console.ReadKey();
}
public static void SaveXML(IDictionary<String, List<String>> infos)
{
if (infos == null || infos.Count == 0)
{
return;
}
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(dec);
XmlElement _infos = xmlDoc.CreateElement("infos");
foreach (KeyValuePair<String, List<String>> item in infos)
{
XmlElement info = xmlDoc.CreateElement("info");
XmlElement name = xmlDoc.CreateElement("file1");
name.InnerText = item.Key;
info.AppendChild(name);
XmlNode filelist = xmlDoc.CreateElement("filelist");
info.AppendChild(filelist);
foreach (String number in item.Value)
{
XmlElement filed = xmlDoc.CreateElement("filed");
filed.InnerText = number;
filelist.AppendChild(filed);
}
_infos.AppendChild(info);
}
xmlDoc.AppendChild(_infos);
xmlDoc.Save(xmlPath);
}
public static IDictionary<String, List<String>> ReadXML()
{













安全验证
文档复制为VIP权益,开通VIP直接复制

评论0