I've never worked with XML and TreeView or the XML control before, following some online tutorials I've cobbled together a working TreeView and XML control but I have several issues: 1) The TreeView is showing the actual name of the node in addition to the text, 2) The XML control is showing all of the pages at once when I want to show only one at a time, 3) How do I configure the TreeView to have the XML control change pages?
The XSLT stylesheet for the XML control:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<book>
<xsl:for-each select="book/page">
<div class="content">
<div class="nav">
<a class="prev" id="prev">Previous Page</a>
<span class="sep">|</span>
<div class="title" id="divTitle">
<xsl:value-of select="title" />
</div>
<span class="sep2">|</span>
<a class="next" id="next">Next Page</a>
</div>
<div class="main">
<xsl:value-of select="content"/>
</div>
<div id="thisPage" class="page">
<xsl:value-of select="pgno" />
</div>
</div>
</xsl:for-each>
</book>
</xsl:template>
</xsl:stylesheet>
The XSLT style sheet for the TreeView:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/book">
<xsl:for-each select="book/page/title">
<xsl:value-of select="title" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The XML file:
<book time="" title="">
<page time="">
<title></title>
<content>
</content>
<pgno></pgno>
</page>
</book>
I'm loading the TreeView and XML control in the code behind like this:
Page_Load:
XmlDocument doc = new XmlDocument();
doc.Load(myXml);
tvBook.Nodes.Clear();
XmlNodeList nodelist = doc.SelectNodes("book/page/title");
XmlDocument cDoc = new XmlDocument();
cDoc.LoadXml("<title></title>");
foreach (XmlNode node in nodelist)
{
XmlNode newElem = cDoc.CreateNode(XmlNodeType.Element, node.Name, node.LocalName);
newElem.InnerText = node.InnerText;
cDoc.DocumentElement.AppendChild(newElem);
}
tvBook.Nodes.Add(new TreeNode(cDoc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = tvBook.Nodes[0];
AddNode(cDoc.DocumentElement, tNode);
tvBook.ExpandAll();
xmlContent.DocumentSource = myXml;
xmlContent.TransformSource = myStyle;
Additional method:
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i < nodeList.Count; i++)
{
xNode = nodeList.Item(i);
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.ChildNodes[i];
AddNode(xNode, tNode);
}
}
else
{
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}
My output on the screen looks like this:
TreeView
title
title
First Page of My Book
title
Second Page of My Book
XML Control
Previous Page TITLE Next Page
CONTENT
Page No
RESOLUTION UPDATE: Because of some additional issues that forced how I achieve my objective to change problems 2 and 3 no longer need to be solved. However, I am marking the below answer as correct because it did resolve problem 1 for me.