简体   繁体   English

如何从XML文件C#创建表并使用Java脚本调用

[英]How To Create a table From XML file C# and Call using Javascript

First of all, I am still a beginner in coding (especially C# and dotnet environment). 首先,我仍然是编码的初学者(尤其是C#和dotnet环境)。 I am trying to create a table contains data get from an XML file. 我正在尝试创建一个表,该表包含从XML文件获取的数据。 In order to access the data from the XML file, credentials are needed. 为了从XML文件访问数据,需要凭据。 The codes to access the XML is done at the codefile. 访问XML的代码在代码文件中完成。 The thing is, the XML file is displaying when I run the code, but I want to display the data into a table when I select an element for example "Title" 事实是,运行代码时正在显示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;
    }
}

Above are the codes, and below is the front end 上面是代码,下面是前端

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

</form>

Is there any way to call some of the xml data and display it in a table? 有什么方法可以调用某些xml数据并将其显示在表中吗?

Whether my understanding is correct , you need to display particular xml data in HTML page. 我的理解是否正确,您需要在HTML页面中显示特定的xml数据。

if its, Here is your answer. 如果是,这是您的答案。

Please Go through this website for reference. 请通过该网站进行参考。

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

Store the data in ==>>>> ViewBag["any_name"]= data; 将数据存储在== >>>> ViewBag [“ any_name”] =数据中;

Then call ViewBag in html Page. 然后在html Page中调用ViewBag。

I hope its usefull to you 我希望它对您有用

Thank u 感谢你

There are many ways to deal with XML responses and you may find many tutorial related to work with XML in C#. 有很多处理XML响应的方法,您可能会发现许多与C#中使用XML相关的教程。 As per your requirement I am providing you simple solution for your current problem. 根据您的要求,我为您提供当前问题的简单解决方案。

Default.aspx Default.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 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();
            }
        }
    }
}

In the above example I have made a call to a fake rest API " https://fakerestapi.azurewebsites.net/api/Authors " which returns data in XML format. 在上面的示例中,我调用了一个假的rest API“ https://fakerestapi.azurewebsites.net/api/Authors ”,该API返回XML格式的数据。 I have mentioned the type of data I want with this code 我已经用这段代码提到了我想要的数据类型

httpWebRequest.ContentType = "application/xml";

DataSet class can holds tables in it we are reading XML response and storing it into dataSet. DataSet类可以在其中保存表,我们正在读取XML响应并将其存储到dataSet中。 Since the response only contains one xml document it generates one table within dataset which we are accessing using Tables(0); 由于响应仅包含一个xml文档,因此它在数据集中生成一个表,我们使用Tables(0)访问该表; and finally passing the table to a grid view which shows data in table format in HTML. 最后将表格传递到网格视图,该视图以HTML表格格式显示数据。 You can design the HTML table later on. 您可以稍后设计HTML表。

You can further learn about DataSet and DataTables class,also about grid view for better understanding. 您可以进一步了解DataSet和DataTables类,还可以了解网格视图以更好地理解。

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM