日期:2014-05-17 浏览次数:20663 次
<?xml version="1.0" encoding="utf-8"?> <nodes> <node> <nodecode>1</nodecode> </node> <node> <nodecode>2</nodecode> </node> </nodes>
------解决方案--------------------
<asp:XmlDataSource runat="server" ID="xmlDataSource1" DataFile="test.xml"></asp:XmlDataSource>
1.设置好DataFile
2.后台更新xml操作
test.xml
<?xml version="1.0" encoding="utf-8"?> <nodes> <node> <nodecode>1</nodecode> </node> <node> <nodecode>2</nodecode> </node> </nodes>
------解决方案--------------------
private void btnSave_Click(object sender, EventArgs e)
{
string SaveFile = @"c:\\test.xml";
sr = new StreamWriter(SaveFile, false, System.Text.Encoding.UTF8);
sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sr.WriteLine("<root>");
GetXml(treeView1.Nodes);
sr.WriteLine("</root>");
sr.Close();
}
StreamWriter sr;
/// 递归Treeview写入XML文件
private void GetXml(TreeNodeCollection tnc)
{
foreach (TreeNode node in tnc)
{
if (node.Parent != null)
{
sr.WriteLine("<A>");
sr.WriteLine("<Name>" + node.Text + "</Name>");
GetXml(node.Nodes);
sr.WriteLine("</A>");
}
else
GetTvXml(node.Nodes);
}
}
http://www.cnblogs.com/wf5360308/articles/1227700.html
------解决方案--------------------
XmlDocument xmlDoc = new XmlDocument();
xmlDoc = (XmlDocument)this.xmlDataSource1.GetXmlDocument();
XmlNode node = xmlDoc.SelectSingleNode("//nodes/node/nodecode[text()='2']");//test为2的nodecode节点
if(node!=null)
{
node.InnerText = "222";//修改节点值
}
this.xmlDataSource1.Save();//保存test.xml
====
你修改一下xmlDataSource1的XmlDocument直接保存就可以了,何必循环遍历treeview再保存呢?
------解决方案--------------------
http://www.cnblogs.com/wf5360308/articles/1227700.html