C# 读写XML文件实例代码
作者:OmySql 时间:2023-04-01 03:40:58
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()
{
IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();
if (File.Exists(xmlPath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlNode xn = xmlDoc.SelectSingleNode("infos");
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
XmlNode nameNode = xe.SelectSingleNode("file1");
string name = nameNode.InnerText;
Console.WriteLine(name);
XmlNode filelist = xe.SelectSingleNode("filelist");
List<String> list = new List<string>();
foreach (XmlNode item in filelist.ChildNodes)
{
list.Add(item.InnerText);
}
infos.Add(name, list);
}
}
return infos;
}
}
}
内容扩展:
实例代码
dim domxmldocument as system.xml.xmldocument
dim tmppath as string = apptempfilepath
dim xmlfile as string = tmppath + "\testxml.xml"
'窗体加载事件
private sub testxml_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
'读xml过程测试通过
dim domxmldocument as system.xml.xmldocument
dim tmppath as string = apptempfilepath
dim xmlfile as string = tmppath + "\testxml.xml"
dim reader as system.xml.xmlreader = nothing
try
reader = new xml.xmltextreader(xmlfile)
'reader.
while reader.read
me.lboxxml.items.add(reader.name + reader.value)
end while
catch ex as exception
msgbox(ex.message)
finally
if not (reader is nothing) then
reader.close()
end if
end try
end sub
'载入xml事件
private sub btnxmlload_click(byval sender as system.object, byval e as system.eventargs) handles btnxmlload.click
'me.lboxxml.items.clear()
''读xml过程测试通过
'dim reader as system.xml.xmlreader = nothing
'try
' reader = new xml.xmltextreader(xmlfile)
' while reader.read
' me.lboxxml.items.add(reader.name + ":" + reader.value)
' end while
'catch ex as exception
' msgbox(ex.message)
'finally
' if not (reader is nothing) then
' reader.close()
' end if
'end try
dim ds as new dataset
try
'如果直接使用ds做datasource则不会展开datagrid,用dv则能直接显示正确。
ds.readxml(xmlfile)
dim tb as datatable
dim dv as dataview
tb = ds.tables(0)
dv = new dataview(tb)
datagrid1.datasource = dv
'datagrid1.datamember = "testxmlmember"
'datagrid1.datamember = "employeefname"
'dim dxd as new xmldatadocument
catch ex as exception
msgbox(ex.message.tostring)
end try
end sub
'保存新建xml内容事件
private sub btnsavenew_click(byval sender as system.object, byval e as system.eventargs) handles btnsavenew.click
dim mytw as new xmltextwriter(tmppath + "\testxmlwrite.xml", nothing)
mytw.writestartdocument()
mytw.formatting = formatting.indented
mytw.writestartelement("team")
mytw.writestartelement("player")
mytw.writeattributestring("name", "george zip")
mytw.writeattributestring("position", "qb")
mytw.writeelementstring("nickname", "zippy")
mytw.writeelementstring("jerseynumber", xmlconvert.tostring(7))
mytw.writeendelement()
mytw.writeendelement()
mytw.writeenddocument()
mytw.close()
end sub
文件很大的情况下,可以考虑手动实现数据更新适配器,比如手动实现一个xml节点搜索/更新,这样就不用重写整个xml。
如果程序的i/o不是主要问题,还是用实体类整个的写入更新吧,毕竟数据的完整性是第一位的。
如是文章类的,对该目录建一个xml索引文件来存放文章的编号,url等,用xml的attribute作为标记不同字段,内容页面可以用另外的html或xml页面存放,用linq to xml操作数据,效率不是很差,个人观点。当搜索时候只要查询指定文件名xml或文件类型就可以了。
来源:https://www.cnblogs.com/OmySql/p/12405569.html
标签:C#,读写XML
0
投稿
猜你喜欢
Android自定义view实现列表内左滑删除Item
2021-10-28 17:19:50
详解SpringBoot2.0的@Cacheable(Redis)缓存失效时间解决方案
2023-07-23 07:20:54
Android实现图片点击爆炸效果
2023-06-04 04:18:55
基于Nacos实现Spring Cloud Gateway实现动态路由的方法
2022-04-14 07:18:56
Spring Boot打war包的实例教程
2022-10-05 07:52:49
c#基于winform制作音乐播放器
2023-08-06 11:55:08
C# 调用Delphi dll 实例代码
2023-06-12 22:46:06
浅谈web服务器项目中静态请求和动态请求处理
2022-03-01 21:26:12
SpringBoot加载读取配置文件过程详细分析
2021-12-29 03:07:36
Java实现班级管理系统
2022-05-08 07:29:48
C语言与C++中关于字符串使用的比较
2022-01-22 01:30:37
C#关闭指定名字进程的方法
2021-11-23 23:59:42
C语言实现扫雷小游戏的示例代码
2022-05-21 13:05:18
c# 获得当前绝对路径的方法(超简单)
2021-06-18 05:23:31
MyBatis的逆向工程详解
2022-12-03 11:06:10
基于C#实现语音识别功能详解
2023-07-12 18:55:24
Android实现可复用的选择页面
2022-09-25 21:08:24
Java技能点之SimpleDateFormat进行日期格式化问题
2023-09-09 01:50:26
Spring Boot + Vue 前后端分离项目如何踢掉已登录用户
2021-07-23 21:57:35
vscode使用官方C/C++插件无法进行代码格式化问题
2022-07-13 06:24:47