如何将treeview节点保存到xml文件C# [英] How can I save treeview node to xml file C#
问题描述
您好,
我正在使用我的第一个Winforms应用程序,然后我需要在树视图中选择或检查一些节点并将它们保存在一个新的xml文件中(或者现有的)
我的XML文件来源是这样的:
<? xml version = 1.0 encoding = UTF-8 ? >
< object_pool rt = 2012 ez = 123 >
< category active = 0 attribut1 = vrai titre = aaaaaa >
< category_value titre = acac >
< object background_colour = 230 id = 0 名称 = zzzzzz type = 0 >
< child id = 121 x = 2 y = 0 / >
< child id = < span class =code-keyword> 131 x = 3 y = 0 / >
< / object >
< / category_value >
< ; /< category >
< / object_pool >
那么对于exp我想要只选择对象或只有孩子或某些对象如果我有很多...
am usi这个函数但它引用了我所有的节点:
private StreamWriter sr;
public void exportToXml(TreeView tv, string filename)
{
sr = new StreamWriter(filename, false ,System.Text.Encoding.UTF8);
// 写标题
sr.WriteLine( <?xml version = \1.0 \encoding = \utf-8 \?>跨度>);
// 写我们的根节点
sr.WriteLine( < + treeView1.Nodes [ 0 ]。文本+ >);
foreach (TreeNode节点 in tv.Nodes)
{
saveNode(node.Nodes);
}
// 关闭根节点
sr.WriteLine ( < / + treeView1.Nodes [ 0 ]。文字+ >);
sr.Close();
}
private void saveNode(TreeNodeCollection tnc)
{
foreach (TreeNode节点 in tnc)
{
// 如果我们有子节点,我们将编写
// 父节点,然后遍历
// 子项
如果(node.Nodes.Count > 0 )
{
sr.WriteLine( < + node.Text + >中跨度>);
saveNode(node.Nodes);
// sr.WriteLine(<+ node.Text +>);
sr.WriteLine( < + node.Name + >);
}
其他 // 没有子节点,所以我们只写文本
{
sr.WriteLine(node.Text);
}
}
}
我真的需要这样做,谢谢大家的帮助
不要使用streamwriter来创建XML文件。使用 .NET XmlWriter类 [ ^ ]。
如果您已在树视图中使用复选框,则可以使用属性已选中
。
赞
foreach (TreeNode节点 in tnc)
{
if (node.Nodes.Count > 0 )
{
if ( node.Nodes.Checked) // 在此行设置断点
继续跨度>;
// 其余代码
}
}
如解决方案1中所述,您应该使用XmlWriter甚至 XDocument [ ^ ](LINQ)
你得到的好处是你不必费心写<和>明确地说。
创建格式良好的XML文档要容易得多。
阅读文档,你很快就会看到光。
如果你想只保存 当前的Checked Nodes(无论它们出现在哪里),那么我建议你跟踪哪些是Checked / UnChecked由用户在运行时,然后只保存那些节点://保存当前的已检查节点
私有列表< TreeNode> CheckedNodes = new List< TreeNode>();
private void TVChecked_AfterCheck(object sender,TreeViewEventArgs e)
{
TreeNode checkChangedNode = e.Node;
if(checkChangedNode.Checked)
{
if(!CheckedNodes.Contains(checkChangedNode))CheckedNodes.Add(checkChangedNode);
}
else
{
if(CheckedNodes.Contains(checkChangedNode))CheckedNodes.Remove(checkChangedNode);
}
}
//假设此Button将调用代码来保存Checked Nodes
private void btnSaveCheckedNodes_Click(object sender,EventArgs e)
{
//没有要检查的节点?
if(CheckedNodes.Count == 0)
{
//抛出错误?回来?
返回;
}
//你可以一次性保存整个CheckedNodes列表吗?
//一个一个地保存?
// foreach(CheckedNodes中的TreeNode tNode)
// {
//保存每个Checked Node
//}
}
Hello ,
Am working on my first Winforms application , then I need to select or check some nodes in treeview and save them in a new xml file ( or existing one )
My XML file source is like this one :
<?xml version="1.0" encoding="UTF-8"?>
<object_pool rt="2012" ez= "123">
<category active="0" attribut1="vrai" titre="aaaaaa">
<category_value titre="acac">
<object background_colour="230" id="0" name="zzzzzz" type="0">
<child id="121" x="2" y="0" />
<child id="131" x="3" y="0" />
</object>
</category_value>
</<category>
</object_pool>
then for exp I want to choose only object or only child or some objects if i have a lot ...
am using this function but it refer to me all nodes :
private StreamWriter sr;
public void exportToXml(TreeView tv, string filename)
{
sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
//Write the header
sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
//Write our root node
sr.WriteLine("<" + treeView1.Nodes[0].Text + ">");
foreach (TreeNode node in tv.Nodes)
{
saveNode(node.Nodes);
}
//Close the root node
sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");
sr.Close();
}
private void saveNode(TreeNodeCollection tnc)
{
foreach (TreeNode node in tnc)
{
// If we have child nodes, we'll write
// a parent node, then iterate through
// the children
if (node.Nodes.Count > 0)
{
sr.WriteLine("<" + node.Text + ">");
saveNode(node.Nodes);
//sr.WriteLine("<" + node.Text + ">");
sr.WriteLine("<" + node.Name + ">");
}
else //No child nodes, so we just write the text
{
sr.WriteLine(node.Text);
}
}
}
I really need to do it , thanks u all for help
Don't use streamwriter to create XML files. Use the .NET XmlWriter class[^].
If you already use check boxes in your tree view, you can use the propertyChecked
.
Like
foreach (TreeNode node in tnc) { if (node.Nodes.Count > 0) { if (node.Nodes.Checked) // Set a breakpoint on this line continue; // The rest of your code } }
As stated in Solution 1, you should use XmlWriter or even XDocument [^](LINQ)
The benefit you get is that you don't have to bother about writing < and > explicitly.
And it will be much easier to create a well formed XML document.
Read the documentation and you will soon see the light.
If you want to save only the current Checked Nodes (wherever they occur), then I suggest you keep track of which ones are Checked/UnChecked by the user at run-time, and then save only those Nodes:// holds the current Checked Nodes private List<TreeNode> CheckedNodes = new List<TreeNode>(); private void TVChecked_AfterCheck(object sender, TreeViewEventArgs e) { TreeNode checkChangedNode = e.Node; if (checkChangedNode.Checked) { if (! CheckedNodes.Contains(checkChangedNode)) CheckedNodes.Add(checkChangedNode); } else { if (CheckedNodes.Contains(checkChangedNode)) CheckedNodes.Remove(checkChangedNode); } } // assume this Button will call the code to save the Checked Nodes private void btnSaveCheckedNodes_Click(object sender, EventArgs e) { // no Checked Nodes to save ? if(CheckedNodes.Count == 0) { // throw an error ? return ? return; } // can you save the whole CheckedNodes List in one go ? // save them one-by-one ? //foreach (TreeNode tNode in CheckedNodes) //{ // save each Checked Node //} }
这篇关于如何将treeview节点保存到xml文件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!