资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#读写XML文件实例代码-创新互联

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

坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都轻质隔墙板小微创业公司专业提供成都企业网站建设营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。
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;
    }
  }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:C#读写XML文件实例代码-创新互联
网站链接:http://cdkjz.cn/article/ejeii.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220