C# 读写XML文件实例代码
C#史上最简单读写xml文件方式,创建控制台应用程序赋值代码,就可以运行,需要改动,请自行调整
usingSystem;
usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Xml;
namespaceConsoleApp1
{
classProgram
{
publicconstStringxmlPath="info.xml";
staticvoidMain(string[]args)
{
IDictionary>infos=newDictionary>();
infos.Add("Evan",newList(){"123","456"});
SaveXML(infos);
ReadXML();
Console.ReadKey();
}
publicstaticvoidSaveXML(IDictionary>infos)
{
if(infos==null||infos.Count==0)
{
return;
}
XmlDocumentxmlDoc=newXmlDocument();
XmlDeclarationdec=xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
xmlDoc.AppendChild(dec);
XmlElement_infos=xmlDoc.CreateElement("infos");
foreach(KeyValuePair>itemininfos)
{
XmlElementinfo=xmlDoc.CreateElement("info");
XmlElementname=xmlDoc.CreateElement("file1");
name.InnerText=item.Key;
info.AppendChild(name);
XmlNodefilelist=xmlDoc.CreateElement("filelist");
info.AppendChild(filelist);
foreach(Stringnumberinitem.Value)
{
XmlElementfiled=xmlDoc.CreateElement("filed");
filed.InnerText=number;
filelist.AppendChild(filed);
}
_infos.AppendChild(info);
}
xmlDoc.AppendChild(_infos);
xmlDoc.Save(xmlPath);
}
publicstaticIDictionary>ReadXML()
{
IDictionary>infos=newDictionary>();
if(File.Exists(xmlPath))
{
XmlDocumentxmlDoc=newXmlDocument();
xmlDoc.Load(xmlPath);
XmlNodexn=xmlDoc.SelectSingleNode("infos");
XmlNodeListxnl=xn.ChildNodes;
foreach(XmlNodexnfinxnl)
{
XmlElementxe=(XmlElement)xnf;
XmlNodenameNode=xe.SelectSingleNode("file1");
stringname=nameNode.InnerText;
Console.WriteLine(name);
XmlNodefilelist=xe.SelectSingleNode("filelist");
Listlist=newList();
foreach(XmlNodeiteminfilelist.ChildNodes)
{
list.Add(item.InnerText);
}
infos.Add(name,list);
}
}
returninfos;
}
}
}
内容扩展:
实例代码
dimdomxmldocumentassystem.xml.xmldocument
dimtmppathasstring=apptempfilepath
dimxmlfileasstring=tmppath+"\testxml.xml"
'窗体加载事件
privatesubtestxml_load(byvalsenderassystem.object,byvaleassystem.eventargs)handlesmybase.load
'读xml过程测试通过
dimdomxmldocumentassystem.xml.xmldocument
dimtmppathasstring=apptempfilepath
dimxmlfileasstring=tmppath+"\testxml.xml"
dimreaderassystem.xml.xmlreader=nothing
try
reader=newxml.xmltextreader(xmlfile)
'reader.
whilereader.read
me.lboxxml.items.add(reader.name+reader.value)
endwhile
catchexasexception
msgbox(ex.message)
finally
ifnot(readerisnothing)then
reader.close()
endif
endtry
endsub
'载入xml事件
privatesubbtnxmlload_click(byvalsenderassystem.object,byvaleassystem.eventargs)handlesbtnxmlload.click
'me.lboxxml.items.clear()
''读xml过程测试通过
'dimreaderassystem.xml.xmlreader=nothing
'try
'reader=newxml.xmltextreader(xmlfile)
'whilereader.read
'me.lboxxml.items.add(reader.name+":"+reader.value)
'endwhile
'catchexasexception
'msgbox(ex.message)
'finally
'ifnot(readerisnothing)then
'reader.close()
'endif
'endtry
dimdsasnewdataset
try
'如果直接使用ds做datasource则不会展开datagrid,用dv则能直接显示正确。
ds.readxml(xmlfile)
dimtbasdatatable
dimdvasdataview
tb=ds.tables(0)
dv=newdataview(tb)
datagrid1.datasource=dv
'datagrid1.datamember="testxmlmember"
'datagrid1.datamember="employeefname"
'dimdxdasnewxmldatadocument
catchexasexception
msgbox(ex.message.tostring)
endtry
endsub
'保存新建xml内容事件
privatesubbtnsavenew_click(byvalsenderassystem.object,byvaleassystem.eventargs)handlesbtnsavenew.click
dimmytwasnewxmltextwriter(tmppath+"\testxmlwrite.xml",nothing)
mytw.writestartdocument()
mytw.formatting=formatting.indented
mytw.writestartelement("team")
mytw.writestartelement("player")
mytw.writeattributestring("name","georgezip")
mytw.writeattributestring("position","qb")
mytw.writeelementstring("nickname","zippy")
mytw.writeelementstring("jerseynumber",xmlconvert.tostring(7))
mytw.writeendelement()
mytw.writeendelement()
mytw.writeenddocument()
mytw.close()
endsub
文件很大的情况下,可以考虑手动实现数据更新适配器,比如手动实现一个xml节点搜索/更新,这样就不用重写整个xml。
如果程序的i/o不是主要问题,还是用实体类整个的写入更新吧,毕竟数据的完整性是第一位的。
如是文章类的,对该目录建一个xml索引文件来存放文章的编号,url等,用xml的attribute作为标记不同字段,内容页面可以用另外的html或xml页面存放,用linqtoxml操作数据,效率不是很差,个人观点。当搜索时候只要查询指定文件名xml或文件类型就可以了。
到此这篇关于C#读写XML文件实例代码的文章就介绍到这了,更多相关C#读写XML文件最简单方法内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。