栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C# 读写XML文件实例代码

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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> infos = new Dictionary>();

      infos.Add("Evan", new List() { "123", "456" });

      SaveXML(infos);

      ReadXML();
      Console.ReadKey();
    }

    public static void SaveXML(IDictionary> 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> 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> ReadXML()
    {
      IDictionary> infos = new Dictionary>();

      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 list = new List();

   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或文件类型就可以了。

到此这篇关于C# 读写XML文件实例代码的文章就介绍到这了,更多相关C# 读写XML文件最简单方法内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122229.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号