XML 解析和树视图

发布于 2024-12-17 07:35:32 字数 1154 浏览 0 评论 0原文

解析 xml 文件:

XmlTextReader reader = new XmlTextReader(fileName);
while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element: // The node is an element.
            Console.Write("<" + reader.Name);
            Console.WriteLine(">");
            break;
        case XmlNodeType.Text: //Display the text in each element.
            Console.WriteLine(reader.Value);
            break;
        case XmlNodeType.EndElement: //Display the end of the element.
            Console.Write("</" + reader.Name);
            Console.WriteLine(">");
            break;
    }
}

我现在如何输入节点来获取我正在解析的 xml 文件的树视图?这就是我到目前为止所拥有的:

<TreeView Margin="177,61" Name="myTreeView" ItemsSource="{Binding XPath=XmlNodeType.Element}" >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding XPath=XmlNodeType.Element}">
            <TextBlock Text="{Binding XPath=XmlNodeType.Text}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

但没有任何显示......只是不明白如何添加节点我猜......

parse the xml file:

XmlTextReader reader = new XmlTextReader(fileName);
while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element: // The node is an element.
            Console.Write("<" + reader.Name);
            Console.WriteLine(">");
            break;
        case XmlNodeType.Text: //Display the text in each element.
            Console.WriteLine(reader.Value);
            break;
        case XmlNodeType.EndElement: //Display the end of the element.
            Console.Write("</" + reader.Name);
            Console.WriteLine(">");
            break;
    }
}

How do i now enter nodes to get a tree view of the xml file i'm parsing? This is what i have thus far:

<TreeView Margin="177,61" Name="myTreeView" ItemsSource="{Binding XPath=XmlNodeType.Element}" >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding XPath=XmlNodeType.Element}">
            <TextBlock Text="{Binding XPath=XmlNodeType.Text}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

but nothing shows up.... just don't understand how to add nodes i guess...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆 2024-12-24 07:35:32

您必须在代码后面添加节点,如下所示:

var newNode = new TreeViewItem
{
    Header = "New Node"
};
myTreeView.Items.Add(newNode);

这会将一个节点添加到树的顶部。如果你想构建一个层次结构,你必须在适当的位置添加节点,如下所示:

// Find the node you want to have as parent.
var parentNode = myTreeView.Items.Cast<TreeViewItem>().FirstOrDefault(item => item.Header = "Parent Node");
if (parentNode != null)
{
    var newNode = new TreeViewItem
    {
        Header = "New Node"
    };
    parentNode.Items.Add(newNode);
}

顺便说一句:我会使用 x:Name 属性来命名 Xaml 中的元素,有时会出现一些问题仅使用 Name 时的代码隐藏

You have to add the nodes in code behind like that:

var newNode = new TreeViewItem
{
    Header = "New Node"
};
myTreeView.Items.Add(newNode);

This adds a node to the top of the tree. If you want to build a hierarchy you have to add the node at the appropiate position like that:

// Find the node you want to have as parent.
var parentNode = myTreeView.Items.Cast<TreeViewItem>().FirstOrDefault(item => item.Header = "Parent Node");
if (parentNode != null)
{
    var newNode = new TreeViewItem
    {
        Header = "New Node"
    };
    parentNode.Items.Add(newNode);
}

BTW: I would use to x:Name Property to name the elements in Xaml, sometimes there is some trouble in code behind when you use only Name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文