服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# 读写XML文件实例代码

C# 读写XML文件实例代码

2022-08-28 16:37OmySql C#

在本篇文章里小编给大家整理的是关于C# 读写XML文件最简单方法,需要的朋友们可以跟着学习参考下。

C#史上最简单读写xml文件方式,创建控制台应用程序赋值代码,就可以运行,需要改动,请自行调整

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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;
    }
  }
}

内容扩展:

实例代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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文件最简单方法内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/OmySql/p/12405569.html

延伸 · 阅读

精彩推荐
  • C#C#远程获取图片文件流的方法

    C#远程获取图片文件流的方法

    这篇文章主要介绍了C#远程获取图片文件流的方法,涉及C#针对图片及文件流操作的相关技巧,需要的朋友可以参考下...

    礼拜一9152021-11-11
  • C#Windows系统中C#读写ini配置文件的程序代码示例分享

    Windows系统中C#读写ini配置文件的程序代码示例分享

    这篇文章主要介绍了C#读写ini配置文件的程序代码示例分享,在Windows下可以利用Win32的API函数轻松实现,需要的朋友可以参考下...

    hzy377412002021-11-18
  • C#C# WPF ListView控件的实例详解

    C# WPF ListView控件的实例详解

    这篇文章主要介绍了C# WPF ListView控件的实例详解的相关资料,希望通过本能帮助到大家,让大家掌握这部分内容,需要的朋友可以参考下...

    weixinhum5502022-01-24
  • C#Unity3D基于OnGUI实时显示FPS

    Unity3D基于OnGUI实时显示FPS

    这篇文章主要介绍了Unity3D基于OnGUI实时显示FPS,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    一缕残阳3362022-08-08
  • C#详解C#的排列组合

    详解C#的排列组合

    本文详细介绍了C#中的排列组合以及具体实现代码,如下所示,希望对大家有所帮助...

    逆心9512021-12-10
  • C#Unity实现卡牌翻动效果

    Unity实现卡牌翻动效果

    这篇文章主要为大家详细介绍了Unity实现卡牌翻动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    杨亚东8422022-03-11
  • C#C#使用正则表达式实现首字母转大写的方法

    C#使用正则表达式实现首字母转大写的方法

    这篇文章主要介绍了C#使用正则表达式实现首字母转大写的方法,涉及C#基于正则表达式操作字符串的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    宁静.致远7642021-11-02
  • C#浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题

    浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题

    下面小编就为大家分享一篇浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题,具有很好的参考价值,希望对大家有所帮助...

    绛河7532022-02-10