0

我在使用 jQuery 读取外部 xml 文件并构建树时遇到了一些问题。为了解决这个问题,我正在编写 C# 代码来读取外部 xml 文件并将数据呈现给 jQuery。到目前为止,这是我的代码:

我想读取外部 xml 文件并按原样打印出来。有人可以帮助我如何按原样打印 xml 文件吗?

   <%@ 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>  

有人可以帮助我如何阅读这个 jstree.xml 并将其呈现给 jQuery。在 jQuery 中,我应该能够做到这一点:

$(document).ready(function () {
    $("#div").jstree({
        "xml_data": {
            "ajax": { 
                "url": "jstree.xml"
4

1 回答 1

0

Jquery 应该向 ASP.NET 页面请求,例如“jstree.aspx”,这个页面 jstree.aspx 应该返回一个 XML 流。

在您的 ASPX 中,在您编写的代码之后:

    Response.Clear();
    Response.ContentType = "text/xml";
    Response.Charset = "utf-8";

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = Encoding.UTF8;
    settings.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(Request.OutputStream, settings))
    { 
       //Write here
    }
于 2012-08-22T18:00:49.917 回答