日期:2014-05-18 浏览次数:21189 次
private void XmlOperation_Load(object sender, EventArgs e) { path = AppDomain.CurrentDomain.BaseDirectory + @"NameList.xml"; xml.Load(path);//加载xml文件 bindTvXml(); } /// <summary> /// 绑定TreeView /// </summary> private void bindTvXml() { for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++) { XmlNode Xnode = xml.DocumentElement.ChildNodes[i]; TreeNode node = new TreeNode(); node.Text = Xnode.Attributes["name"].Value; node.Tag = Xnode; bindChildNode(node, Xnode);//绑定子节点 TvXml.Nodes.Add(node); TvXml.HideSelection = false; } } /// <summary> /// 递归绑定子节点 /// </summary> /// <param name="node"></param> /// <param name="xml"></param> private void bindChildNode(TreeNode node, XmlNode xml) { for (int i = 0; i < xml.ChildNodes.Count; i++) { TreeNode Childnode = new TreeNode(); XmlNode ChildXml = xml.ChildNodes[i]; Childnode.Text = ChildXml.Value; Childnode.Name = "1"; Childnode.Tag = xml.ChildNodes[i]; if (ChildXml.HasChildNodes) { if (ChildXml.ChildNodes[0].NodeType == XmlNodeType.Text) Childnode.Text = ChildXml.ChildNodes[0].InnerText; else bindChildNode(Childnode, ChildXml); } node.Nodes.Add(Childnode); } }
------解决方案--------------------
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"c:\test.xml"); XmlNode node = xmlDoc.SelectSingleNode(@"//Placemark/Name[text()='sole']"); node.ParentNode["LineString"]["coordinates"].InnerXml = "新值"; xmlDoc.Save(@"c:\test.xml");