【问题标题】:create xml file with specific element and tag from a tree c#从树 c# 创建具有特定元素和标签的 xml 文件
【发布时间】:2016-09-24 09:28:51
【问题描述】:

我的 C# windows 应用程序中有一个从 xml 文件加载的树。 我检查了树中的一些节点,然后我想创建并保存一个带有检查树节点的 xml 文件

XML 文件中的示例数据

所选节点必须保存在包含所有元素和属性的 xml 文件中。 谢谢

<menu>
<item id="43BDF924-5E" text="System Management" system="010">
<item id="36A21901-45" text="Basic Information">
<item id="7FA03116-0F" text="Info">
<item id="74713E10-FF" code="AGM-D-1240-01" text="Persons"></item>
<item id="5373F379-E8" code="AGM-D-1260-01" text="Stock"></item>
</item>
</item>
<item id="36A21901-45" text="Google">
<item id="7FA03116-0F" text="sites">
<item id="74876E10-FF" code="MM-D-1240-01" text="Serch"></item>
<item id="0981F379-E8" code="MM-D-1260-01" text="Gmail"></item>
</item>
</item>
</item>
</menu>

【问题讨论】:

  • 欢迎来到 Stack Overflow!发布的所有内容都是程序说明。但是,我们需要您ask a question。我们无法确定您想从我们这里得到什么。请edit您的帖子包含一个我们可以回答的有效问题。提醒:请确保您知道what is on-topic here,请我们为您编写程序,建议是题外话。

标签: c# xml tree


【解决方案1】:

使用 xml linq

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            treeView1.Nodes.AddRange( new TreeNode[] {
                new TreeNode(
                    "id=\"43BDF924-5E\" text=\"System Management\" system=\"010\"", 
                    new TreeNode[] {
                        new TreeNode(
                           "id=\"36A21901-45\" text=\"Basic Information\"",
                           new TreeNode[] {
                               new TreeNode(
                                   "id=\"7FA03116-0F\" text=\"Info\"",
                                   new TreeNode[] {
                                       new TreeNode(
                                           "id=\"74713E10-FF\" code=\"AGM-D-1240-01\" text=\"Persons\""
                                        ),
                                        new TreeNode(
                                            "id=\"5373F379-E8\" code=\"AGM-D-1260-01\" text=\"Stock\""
                                        )
                                   })
                           })
                    }),
                 new TreeNode(
                     "id=\"36A21901-45\" text=\"Google\"",
                     new TreeNode[] {
                         new TreeNode(
                             "id=\"7FA03116-0F\" text=\"sites\"",
                             new TreeNode[] {
                                 new TreeNode(
                                     "id=\"74876E10-FF\" code=\"MM-D-1240-01\" text=\"Serch\""
                                 ),
                                 new TreeNode(
                                     "id=\"0981F379-E8\" code=\"MM-D-1260-01\" text=\"Gmail\""
                                    )
                         })
                      })
            });
            treeView1.ExpandAll();

            XElement menu = new XElement("menu");
            foreach (TreeNode node in treeView1.Nodes)
            {
                XElement newChild = new XElement("item");
                menu.Add(newChild);
                CreateXmlElement(newChild, node);
            }
        }
        public void CreateXmlElement(XElement parentElement, TreeNode parentNode)
        {
            string pattern = "(?'name'\\w+)=\"(?'value'[^\"]+)";
            MatchCollection matches = Regex.Matches(parentNode.Text, pattern);
            foreach (Match match in matches)
            {
                parentElement.Add(new XAttribute(match.Groups["name"].Value, match.Groups["value"].Value));
            }


            foreach (TreeNode node in parentNode.Nodes)
            {
                XElement newChild = new XElement("item");
                parentElement.Add(newChild);
                CreateXmlElement(newChild, node);
            }
        }
    }
}

【讨论】:

  • 感谢您的回答,有没有办法通过代码和动态插入属性。你所做的方式是静态的。如果 Xml 文件被更改,则必须更改代码。如何将xml中所有节点的属性动态插入树中的相应节点。谢谢
  • 它不是静态的,因为我加载了树视图然后只是为了创建测试数据。您所需要的只是读取树视图并保存到 xml 的代码的最后一部分。您可以添加仅保存选中节点的选项。我假设你有加载树视图的代码。
猜你喜欢
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多