C# 获取XML文件内容的多种方式总结
作者:Danny_hi
这篇文章主要介绍了C# 获取XML文件内容的多种方式总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
C#获取XML文件内容的方式
首先设定有一个xml文件Configs.xml,其内容如下:
<?xml version="1.0" encoding="utf-8" ?> <Config> <Device name="aaa"> <Type name="a01" func="1"></Type> <Type name="a02"></Type> <Type name="a03"></Type> <Type name="a04"></Type> </Device> <Device name="bbb"> <Type name="b01"></Type> <Type name="b02"></Type> <Type name="b03"></Type> <Type name="b04"></Type> </Device> <Device name="ccc"> <Type name="c01"></Type> <Type name="c02"></Type> <Type name="c03"></Type> <Type name="c04" func="2"></Type> </Device> </Config>
一、读取外部的XML文件方法
//获取所有一级目录 List<string> devices = new List<string>(); XmlDocument doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\TEMP\Configs.xml"); XmlNode xnl1 = doc.DocumentElement; foreach (XmlNode xn1 in xnl1.ChildNodes) { XmlElement xe1 = (XmlElement)xn1; devices.Add(xe1.GetAttribute("name").ToString()); } return devices;
//获取某个一级目录下的子目录 List<string> types = new List<string>(); XmlDocument doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\TEMP\Configs.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<string> devices = new List<string>(); 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<string> types = new List<string>(); 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<Type> types = new List<Type>(); 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<string> devices= new List<string>(); XmlDocument doc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; XmlReader reader = XmlReader.Create(@"Asserts\Config.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;
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。