昨天看到关于xml的有关操作,在读的过程中,由于是初学者有不明白的地方就查资料,发现自己多innerXml,outerXml,innerXml的理解很不清楚,自己就在原有代码的上进行了部分修改(这不算侵权吧)。首先建立的xml的结构是这样子的,并命名为U-Shop:
< Teams > < Member team = " U-Shop " Description = " U-Shop团队 " > < name > 灵动生活 </ name > < position > planning </ position > </ Member > < Member team = " U-Shop " Description = " U-Shop团队 " > < name > 蓝蓝海天 </ name > < position > support </ position > </ Member > < Member team = " U-Shop " Description = " U-Shop团队 " > < name > 神舟龙 </ name > < position > lean </ position > </ Member > </ Teams > 确定路径:
string myXmlFilePath = " E:\\U-Shop.xml " ; 依照此模板我们有c#代码进行建立,方法代码如下:
// 生成xml 结构 private static void GenerateXMLFIL( string XMLfilePath) { try { // 实例化xml文档 XmlDocument myXmlDoc = new XmlDocument(); // 实例化一个根节点 XmlElement rootElement = myXmlDoc.CreateElement( " Teams " ); // 把根节点添加到xml文档中 myXmlDoc.AppendChild(rootElement); // 创建一个一级节点 XmlElement fistLevelElement1 = myXmlDoc.CreateElement( " Member " ); // 设置属性 fistLevelElement1.SetAttribute( " team " , " U-Shop " ); fistLevelElement1.SetAttribute( " Description " , " U-Shop团队 " ); // 将一级节点添加到根节点 rootElement.AppendChild(fistLevelElement1); // 创建二级节点 XmlElement secondLevelElement11 = myXmlDoc.CreateElement( " name " ); // 设置二级节点的串联值 secondLevelElement11.InnerText = " 灵动生活 " ; // 把二级节点添加到一级几点 fistLevelElement1.AppendChild(secondLevelElement11); XmlElement threeLevelElement12 = myXmlDoc.CreateElement( " position " ); threeLevelElement12.InnerText = " planning " ; fistLevelElement1.AppendChild(threeLevelElement12); XmlElement secondLevelElement2 = myXmlDoc.CreateElement( " Member " ); secondLevelElement2.SetAttribute( " team " , " U-Shop " ); secondLevelElement2.SetAttribute( " Description " , " U-Shop团队 " ); // 添加到根节点 rootElement.AppendChild(secondLevelElement2); XmlElement secondLevelElement21 = myXmlDoc.CreateElement( " name " ); secondLevelElement21.InnerText = " 蓝蓝海天 " ; secondLevelElement2.AppendChild(secondLevelElement21); XmlElement threeLevelElement22 = myXmlDoc.CreateElement( " position " ); threeLevelElement22.InnerText = " support " ; secondLevelElement2.AppendChild(threeLevelElement22); XmlElement secondLevelElement3 = myXmlDoc.CreateElement( " Member " ); secondLevelElement3.SetAttribute( " team " , " U-Shop " ); secondLevelElement3.SetAttribute( " Description " , " U-Shop团队 " ); // 添加到根节点 rootElement.AppendChild(secondLevelElement3); XmlElement secondLevelElement31 = myXmlDoc.CreateElement( " name " ); secondLevelElement31.InnerText = " 神舟龙 " ; secondLevelElement3.AppendChild(secondLevelElement31); XmlElement threeLevelElement32 = myXmlDoc.CreateElement( " position " ); threeLevelElement32.InnerText = " lean " ; secondLevelElement3.AppendChild(threeLevelElement32); // 保存,重要 myXmlDoc.Save(XMLfilePath); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } 下面的方法就是获取已经创建好的xml文件中信息,方法代码如下:
// 获取xml信息 private static void GetXMLInformation( string XMLfilePath) { // 实例化一个xml文档 XmlDocument myXmlDoc = new XmlDocument(); // 加载xml文档 myXmlDoc.Load(XMLfilePath); // 获得第一个为Computers的根节点 XmlNode rootNode = myXmlDoc.SelectSingleNode( " Teams " ); // 获取根节点中串联值 string innertext = rootNode.InnerText.ToString(); // 获取根节点虽有子节点结构 string innerXmlInfo = rootNode.InnerXml.ToString(); // 获取根节点在内的整个xml的结构 string outXmlInfo = rootNode.OuterXml.ToString(); // 获取根节点的所有自己点 // } XmlNodeList firstLevelNodeList = rootNode.ChildNodes; // 遍历所有子节点 foreach (XmlNode node in firstLevelNodeList) { string inner = node.InnerXml; string outx = node.OuterXml; // 获取所有子节点的属性 XmlAttributeCollection attributeCol = node.Attributes; // 遍历自己点属性 foreach (XmlAttribute attri in attributeCol) { // 获取每个节点属性的key,value值 string name = attri.Name; string value = attri.Value; Console.WriteLine( " {0}={1} " , name, value); } // 判断子节点是否还有子节点 if (node.HasChildNodes) { // 如果有子节点则执行 // 获取子节点中的第一个孩子 XmlNode secondeLevelNode1 = node.FirstChild; // 得到第一个孩子的名称 string name = secondeLevelNode1.Name; string innetext = secondeLevelNode1.InnerText.ToString(); string outxml = secondeLevelNode1.OuterXml.ToString(); // 获得第一个孩子的串联值 string innerxml = secondeLevelNode1.InnerXml.ToString(); Console.WriteLine( " {0}={1} " , name, innerxml); XmlNode secondLevelNode2 = node.ChildNodes[ 1 ]; name = secondLevelNode2.Name; innerxml = secondLevelNode2.InnerXml; Console.WriteLine( " {0}={1} " , name, innerxml); } } } 我们很容易获得上边代码中红色字体的值,即innertext=“灵动生活planning蓝蓝海天support神舟龙lean:“理解:根节点以下所有节点的值,innerXmlInfo =“
< Member team = " U-Shop " Description = " U-Shop团队 " > < name > 灵动生活 </ name > < position > planning </ position > </ Member > < Member team = " U-Shop " Description = " U-Shop团队 " > < name > 蓝蓝海天 </ name > < position > support </ position > </ Member > < Member team = " U-Shop " Description = " U-Shop团队 " > < name > 神舟龙 </ name > < position > lean </ position > </ Member > 理解:除根节点标签后,根节点包含的xml结构”, outXmlInfo =”
< Teams > < Member team = " U-Shop " Description = " U-Shop团队 " > < name > 灵动生活 </ name > < position > planning </ position > </ Member > < Member team = " U-Shop " Description = " U-Shop团队 " > < name > 蓝蓝海天 </ name > < position > support </ position > </ Member > < Member team = " U-Shop " Description = " U-Shop团队 " > < name > 神舟龙 </ name > < position > lean </ position > </ Member > </ Teams > 理解:包含根节点标签在内的xml结构“
innertext :可以把这个单词分开进行理解,分开就是inner和text,inner就是里面的意思,这里我们可以理解为不包含本节点,即innertext所在节 点的子节点,text就是文本的意思,所以innertext不会出现标签,整体理解就是innertext所在节点所包含的子节点的值;
innerxml:同样我们也可以把这个单词拆分进行理解,分开就是inner和xml,inner就是里面的意思,这里我们可以理解为不包含本节点,即innerxml所在节点的子节点,xml 这里可以认为是我们所写的标签,当然也包括它的属性值,所以这里理解就是innerxml所在节点所包含的子节点的标签结构;
outerxml:同样我们也可以把这个单词拆分进行理解,分开就是outer和xml,out就是外面的意思,这里我们可以理解为包含本节点,即outerxml所在节点的子节点,xml 这里可以认为是我们所写的标签,当然也包括它的属性值,所以这里理解就是outerxml所在节点以及所包含的子节点的标签结构、属性、值(包含所在节点);
本文转自shenzhoulong 51CTO博客,原文链接:http://blog.51cto.com/shenzhoulong/486880,如需转载请自行联系原作者