xml 读写
来源:互联网 发布:js获取asp控件的值 编辑:程序博客网 时间:2024/06/13 12:45
该内容在C#控制台完成
平台为:Mac
测试时记得修改路径。
using System;using System.Xml;namespace WriteAndReadXml{ class MainClass { static string path = @"/Users/gaolili/Desktop/TestFile/date.xml"; public static void Main(string[] args) { WriteXml(); ReadXml(); Console.WriteLine("Hello World!"); } //写入xml static void WriteXml(){ //创建xml对象 XmlDocument xmd = new XmlDocument(); //定义xml文档元素 XmlElement xmlEle; //定义xml申明段落 XmlDeclaration xmlDec; xmlDec = xmd.CreateXmlDeclaration("1.0", "UTF-8", null); //把该段落写入xml文档中 xmd.AppendChild(xmlDec); //加入一个根元素 xmlEle = xmd.CreateElement("","Employees",""); xmd.AppendChild(xmlEle); for (int i = 0; i < 3; i++) { XmlNode root = xmd.SelectSingleNode("Employees"); XmlElement xel = xmd.CreateElement("Node"); xel.SetAttribute("genre", "DouCube"); xel.SetAttribute("ISBN", "2-3-4"); xel.SetAttribute("name","Gao"); XmlElement xesub1 = xmd.CreateElement("title"); xesub1.InnerText = "U3D Super"; xel.AppendChild(xesub1); XmlElement xesub2 = xmd.CreateElement("author"); xesub2.InnerText = "GaoJin"; xel.AppendChild(xesub2); XmlElement xesub3 = xmd.CreateElement("price"); xesub3.InnerText = (20+i).ToString(); xel.AppendChild(xesub3); root.AppendChild(xel); } //Save xmd.Save(path); } //读取xml static void ReadXml(){ XmlDocument xmd = new XmlDocument(); //加载路径 xmd.Load(path); //获取根节点 XmlNode xn = xmd.SelectSingleNode("Employees"); XmlNodeList xnl = xn.ChildNodes; for (int i = 0; i < xnl.Count; i++) { Console.WriteLine( xnl[i].Attributes.Item(i).Name); Console.WriteLine(xnl[i].Name); Console.WriteLine(xnl[i].InnerText); for (int j = 0; j < xnl[i].ChildNodes.Count; j++) { Console.WriteLine(" child node InnerText : "+xnl[i].ChildNodes.Item(j).InnerText); } } } }}
阅读全文
0 0
- 读写XML
- 读写XML
- 读写XML
- 读写XML
- 读写XML
- xml读写
- XML读写
- 读写XML
- XML读写
- xml读写
- xml 读写
- Xml读写
- XML读写
- xml读写
- 读写XML
- xml读写
- 读写XML
- 读写XML
- [HDU]2829 Lawrence 斜率优化
- 最终解决Mybatis多表联合查询中字段名重复导致的各路问题
- 使用pip安装包
- 朴素贝叶斯分类器——理论
- PowerShell实现简单的grep功能
- xml 读写
- jQuery length 和 size()区别
- Java 标准 I/O 流编程一览笔录
- 7-24 树种统计(25 分)
- -bash: ls: command not found 或ifconfig ,su 等命令不能使用
- TCP分段与IP分片
- Spark算子总结(带案例)
- C++ primer——函数学习
- JFreeChart中文乱码解决方案