1

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.

3
  • See following webpage. I've used the AddNode method a few times before and it works great. stackoverflow.com/questions/28884844/…. Your add node is different. Commented Aug 30, 2015 at 1:56
  • @jdweng The AddNode method I used came from a Microsoft support article here support.microsoft.com/en-us/kb/317597. Can you tell what the difference is between using an XmlNode and passing in an XElement is? How would I pass the XElment to the AddNode method? Commented Aug 30, 2015 at 13:08
  • The Microsoft article is from Visual Studio 5 (2005) which uses a different library from the latest Net Library. The new Net Library was first released with VS 2008 (Net 3.0). The library has changed and the older library code isn't guaranteed to work with the latest VS compiler. Commented Aug 30, 2015 at 13:47

1 Answer 1

1

XDocument is XML Linq while XmlDocument is standard XML.

Try code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
            "<book time=\"\" title=\"\">" +
              "<page time=\"\">" +
                "<title></title>" +
                "<content>" +

                "</content>" +
                "<pgno></pgno>" +
              "</page>" +
            "</book>";


            XDocument doc = XDocument.Parse(input);
            // or from a file
            //XDocument doc = XDocument.Load(filename);

            XElement title = doc.Descendants("title").FirstOrDefault();
        }

    }
}
​
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.