XML 解析和树视图
解析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在代码后面添加节点,如下所示:
这会将一个节点添加到树的顶部。如果你想构建一个层次结构,你必须在适当的位置添加节点,如下所示:
顺便说一句:我会使用
x:Name
属性来命名 Xaml 中的元素,有时会出现一些问题仅使用Name
时的代码隐藏You have to add the nodes in code behind like that:
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:
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 onlyName