C# 获取XML文件内容的多种方式总结

  目录

  C#获取XML文件内容的方式

  首先设定有一个xml文件Configs.xml,其内容如下:

  <?xml version="1.0" encoding="utf-8" ?>

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  一、读取外部的XML文件方法

  //获取所有一级目录

  List devices = new List();

  XmlDocument doc = new XmlDocument();

  doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"TEMPConfigs.xml");

  XmlNode xnl1 = doc.DocumentElement;

  foreach (XmlNode xn1 in xnl1.ChildNodes)

  {

  XmlElement xe1 = (XmlElement)xn1;

  devices.Add(xe1.GetAttribute("name").ToString());

  }

  return devices;

  //获取某个一级目录下的子目录

  List types = new List();

  XmlDocument doc = new XmlDocument();

  doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"TEMPConfigs.xml");

  XmlNode xnl1 = doc.DocumentElement;

  foreach (XmlNode xn1 in xnl1)

  {

  XmlElement xe1 = (XmlElement)xn1;

  if (xe1.GetAttribute("name").ToString() == "aaa")

  {

  XmlNodeList xnl2 = xe1.ChildNodes;

  foreach (XmlNode xn2 in xnl2)

  {

  XmlElement xe2 = (XmlElement)xn2;

  types.Add(xe2.GetAttribute("name").ToString());

  }

  break;

  }

  }

  return types;

  二、读取内部XML文件方法

  文件属性:

  复制到输出目录:不复制

  生成操作:嵌入的资源

  //获取所有一级目录

  List devices = new List();

  string config= System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Configs.Configs.xml";

  System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();

  System.IO.Stream s = assembly.GetManifestResourceStream(config);

  XmlReaderSettings settings = new XmlReaderSettings();

  settings.IgnoreComments = true;

  XmlReader reader = XmlReader.Create(s, settings);

  XmlDocument doc = new XmlDocument();

  doc.Load(reader);

  XmlNode xn = doc.SelectSingleNode("Config");

  XmlNodeList xnl1 = xn.ChildNodes;

  foreach (XmlNode xn1 in xnl1)

  {

  XmlElement xe1 = (XmlElement)xn1;

  devices.Add(xe1.GetAttribute("name").ToString());

  }

  reader.Close();

  return devices;

  //获取某个一级目录下的子目录

  List types = new List();

  string config= System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Configs.Configs.xml";

  System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();

  System.IO.Stream s = assembly.GetManifestResourceStream(config);

  XmlReaderSettings settings = new XmlReaderSettings();

  settings.IgnoreComments = true;

  XmlReader reader = XmlReader.Create(s, settings);

  XmlDocument doc = new XmlDocument();

  doc.Load(reader);

  XmlNode xn = doc.SelectSingleNode("Config");

  XmlNodeList xnl1 = xn.ChildNodes;

  foreach (XmlNode xn1 in xnl1)

  {

  XmlElement xe1 = (XmlElement)xn1;

  if (xe1.GetAttribute("name").ToString() == "aaa")

  {

  XmlNodeList xnl2 = xe1.ChildNodes;

  foreach (XmlNode xn2 in xnl2)

  {

  XmlElement xe2 = (XmlElement)xn2;

  types.Add(xe2.GetAttribute("name").ToString());

  }

  break;

  }

  }

  reader.Close();

  return types;

  三、读取类库中XML文件的方法

  文件属性:

  复制到输出目录:不复制

  生成操作:Resource

  List types = new List();

  string fileName = AppDomain.CurrentDomain.BaseDirectory + "Config.dll";

  System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(fileName);

  string resourceName = assembly.GetName().Name + ".g";

  System.Resources.ResourceManager manager = new System.Resources.ResourceManager(resourceName, assembly);

  object obj = manager.GetObject("asserts/config.xml");//全部字母小写

  System.IO.Stream stream = (System.IO.Stream)obj;

  XmlDocument doc = new XmlDocument();

  XmlReaderSettings settings = new XmlReaderSettings();

  settings.IgnoreComments = true;//忽略文档里面的注释,因为在没有特别说明的情况下,注释也是一个节点

  XmlReader reader = XmlReader.Create(stream, settings);

  doc.Load(reader);

  XmlNode xn = doc.SelectSingleNode("Config");

  XmlNodeList xnl1 = xn.ChildNodes;

  foreach (XmlNode xn1 in xnl1)

  {

  XmlElement xe1 = (XmlElement)xn1;

  Type type = new Type();

  type.name = xe1.GetAttribute("name").ToString();

  type.func = xe1.GetAttribute("func").ToString();

  XmlNodeList xnl0 = xe1.ChildNodes;

  type.Content = xnl0.Item(0).InnerText.Substring(1).TrimEnd();

  types.Add(type);

  }

  reader.Close();

  return types;

  四、读取程序内添加的外部可见的XML文件

  文件属性:

  复制到输出目录:始终复制

  生成操作:内容

  List devices= new List();

  XmlDocument doc = new XmlDocument();

  XmlReaderSettings settings = new XmlReaderSettings();

  settings.IgnoreComments = true;

  XmlReader reader = XmlReader.Create(@"AssertsConfig.xml", settings);

  doc.Load(reader);

  XmlNode xn = doc.SelectSingleNode("Config");

  XmlNodeList xnl1 = xn.ChildNodes;

  foreach (XmlNode xn1 in xnl1)

  {

  XmlElement xe1 = (XmlElement)xn1;

  devices.Add(xe1.GetAttribute("name").ToString());

  }

  reader.Close();

  return serialsList;

  总结

  以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

  您可能感兴趣的文章: