有以下一段Xml:
<?xml version="1.0" encoding="utf-8" ?> <students> <!--我是一段解释笔墨--> <student name="张平"> <courses> <course name="语文?"> <teacherComment> <![CDATA[ 这里是语文先生的讲明 ]]> </teacherComment> </course> <course name="数学"> <teacherComment> <![CDATA[ 这里是数学先生的讲明 ]]> </teacherComment> </course> </courses> </student> </students>
1.怎样运用XmlDocument读取Xml
我要用一段代码遍历一切Student,并打印Student的一切属性和子节点的值
/*玉开博客 http://www.ki4.cn/ */ using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace XmlExample { class Program { static void Main(string[] args) { string xmlFilePath = @"X:\about.net\example\XmlExample\1.xml"; XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); //运用xpath表达式挑选文档中一切的student子节点 XmlNodeList studentNodeList = doc.SelectNodes("/students/student"); if (studentNodeList != null) { foreach (XmlNode studentNode in studentNodeList) { //经由过程Attributes取得属性名字为name的属性 string name = studentNode.Attributes["name"].Value; Console.WriteLine("Student:" + name); //经由过程SelectSingleNode要领取得当前节点下的courses子节点 XmlNode coursesNode = studentNode.SelectSingleNode("courses"); //经由过程ChildNodes属性取得courseNode的一切一级子节点 XmlNodeList courseNodeList = coursesNode.ChildNodes; if (courseNodeList != null) { foreach (XmlNode courseNode in courseNodeList) { Console.Write("\t"); Console.Write(courseNode.Attributes["name"].Value); Console.Write("先生考语"); //经由过程FirstNode属性能够取得课程节点的第一个子节点,LastNode能够取得末了一个子节点 XmlNode teacherCommentNode = courseNode.FirstChild; //读取CData节点 XmlCDataSection cdata = (XmlCDataSection)teacherCommentNode.FirstChild; Console.WriteLine(cdata.InnerText.Trim()); } } } } Console.Write("\r\nPress any key to continue...."); Console.Read(); } } }
XmlDocument自身是从XmlNode继续的,读Xml节点能够经由过程FirstChild,LastChild,或许NextSibling,PreviousSibling读取单个节点,或许经由过程ChildNodes读取一切子节点。还能够运用XPath表达式运用SelectNodes(string xpath)或许SelectSingleNode(string xpath)读取单个或许多个相符前提的节点。
2.怎样经由过程XmlDocument编辑Xml
同样是读取Xml中的xml例子,我们此次要用csharp代码生成xml,以下代码:
/*玉开博客 http://www.ki4.cn/ */ using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace WriteXml { class Program { static void Main(string[] args) { XmlDocument xmlDoc = new XmlDocument(); //建立Xml声明部份,即<?xml version="1.0" encoding="utf-8" ?> xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"); //建立根节点 XmlNode rootNode = xmlDoc.CreateElement("students"); //建立student子节点 XmlNode studentNode = xmlDoc.CreateElement("student"); //建立一个属性 XmlAttribute nameAttribute = xmlDoc.CreateAttribute("name"); nameAttribute .Value = "张同砚"; //xml节点附件属性 studentNode.Attributes.Append(nameAttribute); //建立courses子节点 XmlNode coursesNode = xmlDoc.CreateElement("courses"); XmlNode courseNode1 = xmlDoc.CreateElement("course"); XmlAttribute courseNameAttr = xmlDoc.CreateAttribute("name"); courseNameAttr.Value = "语文"; courseNode1.Attributes.Append(courseNameAttr); XmlNode teacherCommentNode = xmlDoc.CreateElement("teacherComment"); //建立Cdata块 XmlCDataSection cdata = xmlDoc.CreateCDataSection("<font color=\"red\">这是语文先生的讲明</font>"); teacherCommentNode.AppendChild(cdata); courseNode1.AppendChild(teacherCommentNode); coursesNode.AppendChild(courseNode1); //附加子节点 studentNode.AppendChild(coursesNode); rootNode.AppendChild(studentNode); //附加根节点 xmlDoc.AppendChild(rootNode); //保留Xml文档 xmlDoc.Save(@"d:\test.xml"); Console.WriteLine("已保留Xml文档"); } } }
运用XmlDocument生成xml的要点在于运用xmlDocument的实例的CreateElement建立XmlNode或许经由过程CreateAttribute要领建立属性,并经由过程AppendChild要领附加xml节点,经由过程AppendAttribute附加Attribute到节点的属性鸠合。
以上就是详解经由过程XmlDocument读写Xml文档的示例代码的细致内容,更多请关注ki4网别的相干文章!