Xml
容易读懂;格式标准;任何语言都内置XML引擎,不用单独进行文件分析引擎的编写;跨系统传送数据
语法
标签(Tag)、嵌套(Nest)、属性。标签要闭合,属性值要用""包围,标签可以互相嵌套
区分大小写
XML树 ,父节点,节点、兄弟节点
必须要有结束标签,元素属性必须用引号
1 创建
XmlDocument doc=new XmlDocument();
保存
doc.Save("D:\\1.xml");
static void Main(string[] args) { XmlDocument doc = new XmlDocument(); //创建文档声明节点 XmlDeclaration xmldec= doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(xmldec); //创建根节点 XmlElement xmlele = doc.CreateElement("New"); doc.AppendChild(xmlele); XmlElement xmlTitle = doc.CreateElement("Title"); xmlele.AppendChild(xmlTitle); xmlTitle.InnerText = "收回钓鱼岛"; XmlElement xmlContent = doc.CreateElement("Content"); xmlele.AppendChild(xmlContent); xmlContent.InnerText = "日本沉没"; doc.Save("D:\\New.xml"); }
New.xml内容如下
<?xml version="1.0" encoding="utf-8"?> <New> <Title>收回钓鱼岛</Title> <Content>日本沉没</Content> </New>
innerText与innerXml
类似于js中的innerText与innerHtml 对<>等字符的转换
2 更新XML内容 首先判断xml文件是否存在,存在的话追加内容
string path = "D:\\New.xml"; XmlDocument doc = new XmlDocument(); if (!System.IO.File.Exists(path)) { #region 创建XML XmlDeclaration xmldec = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(xmldec); //创建根节点 XmlElement parent = doc.CreateElement("News"); doc.AppendChild(parent); XmlElement san = doc.CreateElement("New"); parent.AppendChild(san); XmlElement xmlTitle = doc.CreateElement("Title"); san.AppendChild(xmlTitle); xmlTitle.InnerText = "收回钓鱼岛"; XmlElement xmlContent = doc.CreateElement("Content"); san.AppendChild(xmlContent); xmlContent.InnerText = "日本沉没111"; #endregion } else { //加载已经存在的xml文件 doc.Load(path); //获取New根节点 XmlElement parent= doc.DocumentElement; XmlElement san = doc.CreateElement("New"); parent.AppendChild(san); #region XML更新(实际使用中可定义方法调用) XmlElement xmlTitle = doc.CreateElement("Title"); san.AppendChild(xmlTitle); xmlTitle.InnerText = "新医改"; XmlElement xmlContent = doc.CreateElement("Content"); san.AppendChild(xmlContent); xmlContent.InnerText = "国家承担90%医药费"; #endregion } doc.Save("D:\\New.xml");
结果:
<?xml version="1.0" encoding="utf-8"?> <New> <Title>收回钓鱼岛</Title> <Content>日本沉没</Content> <Title>新医改</Title> <Content>国家承担90%医药费</Content> </New>
创建带属性的XML节点
#region 创建xml的属性节点 //创建xml的属性节点 XmlDocument Sxjd = new XmlDocument(); XmlDeclaration dec = Sxjd.CreateXmlDeclaration("1.0", "utf-8", null); Sxjd.AppendChild(dec); //根节点 XmlElement xmlRoot = Sxjd.CreateElement("Order"); Sxjd.AppendChild(xmlRoot); XmlElement xml1 = Sxjd.CreateElement("CustomerName"); xml1.InnerText = "购物车"; xmlRoot.AppendChild(xml1); XmlElement xml2 = Sxjd.CreateElement("OrderNumber"); xml2.InnerText = "od00001"; xmlRoot.AppendChild(xml2); //创建Items XmlElement items = Sxjd.CreateElement("Items"); xmlRoot.AppendChild(items); XmlElement orderItems1 = Sxjd.CreateElement("OrderItem"); items.AppendChild(orderItems1); orderItems1.SetAttribute("Name", "电脑"); orderItems1.SetAttribute("count", "30"); items.AppendChild(orderItems1); XmlElement orderItems2 = Sxjd.CreateElement("OrderItem"); items.AppendChild(orderItems2); orderItems2.SetAttribute("Name", "U盘"); orderItems2.SetAttribute("count", "2"); items.AppendChild(orderItems2); Sxjd.Save("d:\\order.xml"); #endregion
结果:
<?xml version="1.0" encoding="utf-8"?> <Order> <CustomerName>购物车</CustomerName> <OrderNumber>od00001</OrderNumber> <Items> <OrderItem Name="电脑" count="30" /> <OrderItem Name="U盘" count="2" /> </Items> </Order>
读取XML文件
string paths = "D:\\New.xml"; XmlDocument docu = new XmlDocument(); docu.Load(paths); XmlNode parents = docu.DocumentElement; XmlNodeList xnl = parents.ChildNodes; foreach (XmlNode node in xnl) { Console.WriteLine(node.ChildNodes[0].InnerText); Console.WriteLine(node.ChildNodes[1].InnerText); } Console.Read();