![](/img/trans.png)
[英]How to add a javascript file (.js file) to a c# project and call a javascript method from c# code
[英]How To Create a table From XML file C# and Call using Javascript
首先,我仍然是编码的初学者(尤其是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数据并将其显示在表中吗?
我的理解是否正确,您需要在HTML页面中显示特定的xml数据。
如果是,这是您的答案。
请通过该网站进行参考。
http://www.tutorialsteacher.com/mvc/viewbag-in-asp.net-mvc
将数据存储在== >>>> ViewBag [“ any_name”] =数据中;
然后在html Page中调用ViewBag。
我希望它对您有用
感谢你
有很多处理XML响应的方法,您可能会发现许多与C#中使用XML相关的教程。 根据您的要求,我为您提供当前问题的简单解决方案。
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
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 ”,该API返回XML格式的数据。 我已经用这段代码提到了我想要的数据类型
httpWebRequest.ContentType = "application/xml";
DataSet类可以在其中保存表,我们正在读取XML响应并将其存储到dataSet中。 由于响应仅包含一个xml文档,因此它在数据集中生成一个表,我们使用Tables(0)访问该表; 最后将表格传递到网格视图,该视图以HTML表格格式显示数据。 您可以稍后设计HTML表。
您可以进一步了解DataSet和DataTables类,还可以了解网格视图以更好地理解。
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.