【问题标题】:How To Create a table From XML file C# and Call using Javascript如何从 XML 文件 C# 创建表并使用 Javascript 调用
【发布时间】:2018-07-04 06:23:48
【问题描述】:

首先,我还是个编码初学者(尤其是C#和dotnet环境)。我正在尝试创建一个包含从 XML 文件获取的数据的表。为了访问 XML 文件中的数据,需要凭据。访问 XML 的代码在代码文件中完成。问题是,当我运行代码时正在显示 XML 文件,但是当我选择一个元素(例如“标题”)时,我想将数据显示到表格中

 protected void Page_Load(object sender, EventArgs e)
{
    String sUsername = "administrator";
    String sPassword = "123";

    String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(sUsername + ":" + sPassword));
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("//pageurl");
    httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
    httpWebRequest.PreAuthenticate = true;

    HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    if (webResponse.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = webResponse.GetResponseStream();
        StreamReader streamReader = new StreamReader(responseStream, Encoding.Default);
        string pageContent = streamReader.ReadToEnd();

        Debug.Print("Web response output as follows:");

        TextArea1.Text = pageContent;
    }
}

上面是代码,下面是前端

<form id="form1" runat="server">
<div>
<asp:TextBox id="TextArea1" TextMode="multiline" Columns="50" Rows="5" runat="server" />
</div>

</form>

有什么方法可以调用部分xml数据并在表格中显示出来?

【问题讨论】:

    标签: javascript c# html asp.net xml


    【解决方案1】:

    无论我的理解是否正确,您需要在 HTML 页面中显示特定的 xml 数据。

    如果是,这就是你的答案。

    请浏览本网站以供参考。

    http://www.tutorialsteacher.com/mvc/viewbag-in-asp.net-mvc

    将数据存储在 ==>>>> ViewBag["any_name"]= data;

    然后在html页面中调用ViewBag。

    希望对你有用

    谢谢你

    【讨论】:

    • 在html中显示xml数据已经完成,通过将xml的页面内容显示到html页面中。现在我需要将一些数据查看到一个表中。我不知道 viewbag 是否可以提供帮助,但我会尝试一下。谢谢
    【解决方案2】:

    有很多方法可以处理 XML 响应,您可以找到许多与在 C# 中使用 XML 相关的教程。根据您的要求,我正在为您当前的问题提供简单的解决方案。

    默认.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ms_tempo.Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="gv" runat="server"></asp:GridView>
        </form>
    </body>
    </html>
    

    Default.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Data;
    
    namespace ms_tempo
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://fakerestapi.azurewebsites.net/api/Authors");
                httpWebRequest.ContentType = "application/xml";
    
                HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    
                DataSet dataSet = new DataSet();
    
                if (webResponse.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = webResponse.GetResponseStream();
    
                    dataSet.ReadXml(responseStream);
    
                    gv.DataSource = dataSet.Tables[0];
                    gv.DataBind();
                }
            }
        }
    }
    

    在上面的示例中,我调用了一个假的 rest API“https://fakerestapi.azurewebsites.net/api/Authors”,它以 XML 格式返回数据。我已经用这段代码提到了我想要的数据类型

    httpWebRequest.ContentType = "application/xml";
    

    DataSet 类可以在其中保存表,我们正在读取 XML 响应并将其存储到 dataSet 中。由于响应仅包含一个 xml 文档,因此它会在数据集中生成一个表,我们使用 Tables(0) 访问该表;最后将表格传递给网格视图,该视图以 HTML 表格格式显示数据。您可以稍后设计 HTML 表格。

    您可以进一步了解 DataSet 和 DataTables 类,也可以了解网格视图以更好地理解。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 2015-11-09
      • 2011-12-19
      相关资源
      最近更新 更多