I ran into some problems using jQuery to read an external xml file and build a tree. To get around it, I am working on C# code to read the external xml file and present the data to the jQuery. Here is my code so far:
I would like to read the external xml file and print it out as is. Can someone help me how to print the xml file as is?
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
string xmlFile = "http://192.168.101.1/img/jstree.xml";
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
Console.WriteLine(xmlFile);
try
{
using (XmlReader reader = XmlReader.Create(xmlFile, settings))
{
string xmlContent;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
xmlContent = "";
if (reader.Name == "root")
{
xmlContent += "<root>" + "<br />" "reader.ReadString().ToString() + "<br />";
}
if (reader.Name == "item id")
{
xmlContent += reader.ReadString().ToString() + "<br />";
}
if (reader.Name == "content")
{
xmlContent += reader.ReadString().ToString() + "<br />";
}
if (reader.Name == "name")
{
xmlContent += reader.ReadString().ToString() + "<br />";
}
Label1.Text += xmlContent;
}
}
}
}
catch (Exception ex)
{
Label1.Text = "An Error Occured: " + ex.Message;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>XmlReader: How to read and process Xml file element data in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="false"
ForeColor="Crimson"
Font-Size="Large"
Font-Names="Comic Sans MS"
>
</asp:Label>
</div>
</form>
</body>
</html>
Can someone help with how do I read this jstree.xml and present it to the jQuery. In jQuery, I should be able to do this:
$(document).ready(function () {
$("#div").jstree({
"xml_data": {
"ajax": {
"url": "jstree.xml"
Jquery should request to ASP.NET Page, like “jstree.aspx”, this page, jstree.aspx, should return a XML Stream.
In your ASPX, after the code you writed: