如何使用WPF从XML文件创建treeview?
这是XML文件如何使用WPF从XML文件创建treeview?,wpf,treeview,Wpf,Treeview,这是XML文件 <Root> <RootNode name="CurrentDbName" value="DeltaTest Backup" DesiPath="E:\BuildBackups"> <ChildNode name="Application" value="App"> <LeafNode name="Source" value="Source" SourcePath="E:\Alertv2" /> <LeafNo
<Root>
<RootNode name="CurrentDbName" value="DeltaTest Backup" DesiPath="E:\BuildBackups">
<ChildNode name="Application" value="App">
<LeafNode name="Source" value="Source" SourcePath="E:\Alertv2" />
<LeafNode name="Publish" value="Publish" SourcePath="C:\Alert_Source" />
</ChildNode>
<ChildNode name="Database" value="DB">
<LeafNode name="Dev" value="Dev" SourcePath="C:\Kiran3" />
<LeafNode name="Build" value="Build" SourcePath="C:\Kiran4" />
</ChildNode>
</RootNode> </Root>
因此,请帮助我创建此树视图。您可以阅读该网站的一个示例:
<TreeView Margin="10,10,0,13" Name="TreeView1" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="194" Height="200">
<TreeViewItem Header="Cold Drinks">
<TreeViewItem Header="Coke"></TreeViewItem>
<TreeViewItem Header="Pepsi"></TreeViewItem>
<TreeViewItem Header="Orange Juice"></TreeViewItem>
<TreeViewItem Header="Milk"></TreeViewItem>
<TreeViewItem Header="Iced Tea"></TreeViewItem>
<TreeViewItem Header="Mango Shake"></TreeViewItem>
</TreeViewItem>
</TreeView>
这将导致这种情况
(来源:)
因此,在您的情况下,您需要添加更多的treeviewitem并将它们嵌套一点,使用上面的代码来获得您想要的结果 欢迎使用stackoverflow,如果您可以发布一些示例xml,它将有助于可视化您的工作方向。我认为您需要使用一个或多个
hierarchycaldatatemplate
假设一个名为data.xml的xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<item>
<DeltaTestBaclup>aaa</DeltaTestBaclup>
<App>bbb</App>
<Source>ccc</Source>
<Publish>ddd</Publish>
</item>
<item>
<DeltaTestBaclup>aaa</DeltaTestBaclup>
<App>bbb</App>
<Source>ccc</Source>
<Publish>ddd</Publish>
</item>
</root>
aaa
bbb
ccc
ddd
aaa
bbb
ccc
ddd
您可以使用类似以下内容的xaml:
<Grid>
<Grid.DataContext>
<XmlDataProvider Source="data.xml"></XmlDataProvider>
</Grid.DataContext>
<Grid.Resources>
<HierarchicalDataTemplate x:Key="ItemTemplate" DataType="item">
<TextBlock>
<TextBlock Text="{Binding XPath=DeltaTestBaclup}" />
<LineBreak></LineBreak>
<TextBlock Text="{Binding XPath=App}" />
<LineBreak></LineBreak>
<TextBlock Text="{Binding XPath=Source}" />
<LineBreak></LineBreak>
<TextBlock Text="{Binding XPath=Publish}" />
</TextBlock>
</HierarchicalDataTemplate>
</Grid.Resources>
<TreeView Name="treeView"
ItemsSource="{Binding Path=.,XPath=/root/item}"
ItemTemplate="{StaticResource ItemTemplate}">
</TreeView>
</Grid>
以下是一种编程方式。这是基于 该代码将根据您的规范构建树。这是XAML
<Grid>
<TreeView Margin="20" Background="LightGray" x:Name="treeView" />
</Grid>
我知道几个月前有人问过这个问题,但我认为答案并不准确,因为我遇到了同样的问题,这些建议都没有帮助我解决这个问题 以下链接解释了如何使用winforms从.xml填充树视图: 我设法使其适应wpf,以便使用.xml中的信息填充树视图:
//First, we'll load the Xml document
XmlDocument xDoc = new XmlDocument();
xDoc.Load(filename);
//Now, clear out the treeview,
treeView.Items.Clear();
//and add the first (root) node
TreeViewItem treeviewItemRoot = new TreeViewItem();
treeviewItemRoot.Header = "Root";
treeView.Items.Add(treeviewItemRoot);
TreeViewItem tNode = new TreeViewItem();
tNode = (TreeViewItem)treeView.Items[0];
//We make a call to addTreeNode,
//where we'll add all of our nodes
addTreeNode(xDoc.DocumentElement, tNode);
//This function is called recursively until all nodes are loaded
private void addTreeNode(XmlNode xmlNode, TreeViewItem treeNode)
{
XmlNode xNode;
TreeViewItem tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes) //The current node has children
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
treeNode.Items.Add(new TreeViewItem());
tNode = treeNode.Items[x] as TreeViewItem;
addTreeNode(xNode, tNode);
}
}
else //No children, so add the outer xml (trimming off whitespace)
treeNode.Header = xmlNode.OuterXml.Trim();
}
//首先,我们将加载Xml文档
XmlDocument xDoc=新的XmlDocument();
加载(文件名);
//现在,清除树景,
treeView.Items.Clear();
//并添加第一个(根)节点
TreeViewItem treeviewItemRoot=新的TreeViewItem();
treeviewItemRoot.Header=“Root”;
treeView.Items.Add(treeviewItemRoot);
TreeViewItem tNode=新的TreeViewItem();
tNode=(treeView项)treeView.Items[0];
//我们打电话给addTreeNode,
//我们将在其中添加所有节点
addTreeNode(xDoc.DocumentElement,tNode);
//递归调用此函数,直到加载所有节点
私有void addTreeNode(XmlNode XmlNode,TreeViewItem treeNode)
{
xmlnodexnode;
树状网节点;
XmlNodeList xNodeList;
if(xmlNode.HasChildNodes)//当前节点有子节点
{
xNodeList=xmlNode.ChildNodes;
对于(int x=0;x
这是每个xml结构的最通用版本。例如:
<Catalog>
<Book id="bk101">
<Author>Garcia, Debra</Author>
<Title id="33">XML Developer's Guide</Title>
<Genre>Computer</Genre>
<Price>44.95</Price>
<PublishDate>2000-10-01</PublishDate>
<Description>An in-depth look at creating applications
with XML.</Description>
</Book>
<Book id="bk102">
<Author>Garcia, Debra</Author>
<Title>Midnight Rain</Title>
<Genre>Fantasy</Genre>
<Price>5.95</Price>
<PublishDate>2000-12-16</PublishDate>
<Description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</Description>
</Book>
</Catalog>
加西亚,黛布拉
XML开发人员指南
电脑类
44.95
2000-10-01
深入了解如何创建应用程序
使用XML。
加西亚,黛布拉
夜雨
幻想
5.95
2000-12-16
一位前建筑师与企业僵尸搏斗,
一个邪恶的女巫,和她自己的童年成为女王
世界的一部分。
将产生以下结果:
嘿,您在“BuildNodes(childTreeNode,childElement)”中漏掉了一个分号:)问题是“如何从XML文件创建treeview”而不是在xaml中手动嵌套如何将它链接到xaml页面?
//First, we'll load the Xml document
XmlDocument xDoc = new XmlDocument();
xDoc.Load(filename);
//Now, clear out the treeview,
treeView.Items.Clear();
//and add the first (root) node
TreeViewItem treeviewItemRoot = new TreeViewItem();
treeviewItemRoot.Header = "Root";
treeView.Items.Add(treeviewItemRoot);
TreeViewItem tNode = new TreeViewItem();
tNode = (TreeViewItem)treeView.Items[0];
//We make a call to addTreeNode,
//where we'll add all of our nodes
addTreeNode(xDoc.DocumentElement, tNode);
//This function is called recursively until all nodes are loaded
private void addTreeNode(XmlNode xmlNode, TreeViewItem treeNode)
{
XmlNode xNode;
TreeViewItem tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes) //The current node has children
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
treeNode.Items.Add(new TreeViewItem());
tNode = treeNode.Items[x] as TreeViewItem;
addTreeNode(xNode, tNode);
}
}
else //No children, so add the outer xml (trimming off whitespace)
treeNode.Header = xmlNode.OuterXml.Trim();
}
class Mapper
{
private string sourceXmlFile;
private XDocument xmlData;
public Mapper(string xmlFilePath)
{
sourceXmlFile = xmlFilePath;
}
private void BuildNodes(TreeViewItem treeNode, XElement element)
{
string attributes = "";
if (element.HasAttributes)
{
foreach (var att in element.Attributes())
{
attributes += " " + att.Name + " = " + att.Value;
}
}
TreeViewItem childTreeNode = new TreeViewItem
{
Header = element.Name.LocalName + attributes,
IsExpanded = true
};
if (element.HasElements)
{
foreach (XElement childElement in element.Elements())
{
BuildNodes(childTreeNode, childElement);
}
}
else
{
TreeViewItem childTreeNodeText = new TreeViewItem
{
Header = element.Value,
IsExpanded = true
};
childTreeNode.Items.Add(childTreeNodeText);
}
treeNode.Items.Add(childTreeNode);
}
public void LoadXml(TreeView treeview)
{
try
{
if (sourceXmlFile != null)
{
xmlData = XDocument.Load(sourceXmlFile, LoadOptions.None);
if (xmlData == null)
{
throw new XmlException("Cannot load Xml document from file : " + sourceXmlFile);
}
else
{
TreeViewItem treeNode = new TreeViewItem
{
Header = sourceXmlFile,
IsExpanded = true
};
BuildNodes(treeNode, xmlData.Root);
treeview.Items.Add(treeNode);
}
}
else
{
throw new IOException("Xml file is not set correctly.");
}
}
catch (IOException ioex)
{
//log
}
catch (XmlException xmlex)
{
//log
}
catch (Exception ex)
{
//log
}
}
}
<Catalog>
<Book id="bk101">
<Author>Garcia, Debra</Author>
<Title id="33">XML Developer's Guide</Title>
<Genre>Computer</Genre>
<Price>44.95</Price>
<PublishDate>2000-10-01</PublishDate>
<Description>An in-depth look at creating applications
with XML.</Description>
</Book>
<Book id="bk102">
<Author>Garcia, Debra</Author>
<Title>Midnight Rain</Title>
<Genre>Fantasy</Genre>
<Price>5.95</Price>
<PublishDate>2000-12-16</PublishDate>
<Description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</Description>
</Book>
</Catalog>