在asp.net网页上显示xml字符串 [英] Display xml string on asp.net webpage

查看:71
本文介绍了在asp.net网页上显示xml字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在当前页面上显示xml字符串方法。我目前正在使用page_load方法呈现XML字符串,但使用此方法,我目前正在遇到一个空白的网页。他们是一个更好的使用方法,以解决这个问题。请进一步建议。



更新代码V2:

  if (!IsPostBack)
{
// string a = createXMLPub(data);
// Label1.Text = a;

createXML(data);

// string a = createXMLPub(data);
// XmlDocument xml = new XmlDocument();
// xml.LoadXml(a);
// < span class =code-comment> Response.Write(xml.OuterXml);
// format_dvi.InnerText = xml.OuterXml;

// 响应。 Clear();
// string a = createXML(data);
// XmlDocument xml = new XmlDocument();
// xml.LoadXml(a);
// 回复.ContentType =text / xml;
// Response.Write(xml.OuterXml );
// Response.End();
}

public void createXML( string data)
{
string a = createXMLPub(data);
XDocument xDoc = new XDocument(a);
Label1.Text = a;
XDocument.Parse(a);
尝试
{
xDoc = XDocument.Load(data);

}
catch (例外情况)
{
Label1.Text = ex.ToString() ;
return ;
}
}





  public   static   string  createXMLPub( string  data)
{
XElement xeRoot = new XElement( < span class =code-string> pub);
XElement xeName = new XElement( name ###);
xeRoot.Add(xeName);
XElement xeCategory = new XElement( category #####);
xeRoot.Add(xeCategory);
XDocument xDoc = new XDocument(xeRoot);
// xDoc.Save(Server.MapPath(products-string2.xml));
// Response.Redirect(products-string2.xml); //加载Web浏览器
// return xDoc;
data = xDoc.ToString ();
返回数据;
}





谢谢

解决方案

它会显示当前页面中的xml,但将清除所有其他内容..

仅显示XML文档

  protected   void  Page_Load( object  sender,EventArgs e)
{
// 清除页面
Response.Clear();
XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath( products-string2.xml));
Response.ContentType = text / xml;
Response.Write(xml.OuterXml);
Response.End();
}







版本2



根据您的更新,我从这里创建了相同的XML

  protected   void  Page_Load( object  sender,EventArgs e)
{


Response.Clear();
string data = string .Empty;
XElement xeRoot = new XElement( pub );
XElement xeName = new XElement( name 测试名称);
xeRoot.Add(xeName);
XElement xeCategory = new XElement( category 测试类别);
xeRoot.Add(xeCategory);
XDocument xDoc = new XDocument(xeRoot);
data = xDoc.ToString();

// 清除页面
Response.ContentType = text / xml;
Response.Write(data);
Response.End();
}





它给我一个XML

 < span class =code-keyword><   pub  >  
< 名称 > 测试名称< span class =code-keyword>< / name >
< category > 测试类别< / category >
< / pub >







版本3



我不知道你传递给函数 crea teXMLPub

你也可以调试一些brakpoint ..



  protected   void  Page_Load( object  sender,EventArgs e)
{
Response.Clear();
string data = createXMLPub( string .Empty);
// 清除页面
Response.ContentType = text / xml;
Response.Write(data);
Response.End();
}

public string createXMLPub( string data)
{
XElement xeRoot = new XElement( pub);
XElement xeName = new XElement( name 测试名称);
xeRoot.Add(xeName);
XElement xeCategory = new XElement( category 测试类别);
xeRoot.Add(xeCategory);
XDocument xDoc = new XDocument(xeRoot);
data = xDoc.ToString();
返回数据;
}


您可以使用XmlTextReader。这很简单。



 xml.InnerText =  ; 
XmlTextReader xlRead = new XmlTextReader(Server.MapPath( library.xml));

while (xlRead.Read())
{
xlRead.MoveToElement();

div_xml.InnerHtml = div_xml.InnerHtml + < br /> + xlRead.Name + + xlRead.Value;
}







使用Asp.Net XmlTextReader读取XML


如果你是试图显示整个XML(而不是其数据)只需使用XmlDocument



  protected   void  Page_Load( object  sender,EventArgs e, string  data)
{
if (!IsPostBack)


XmlDocument xml = new XmlDocument;
xml.LoadXml(data);
your_div.InnerText = xml.OuterXml;
}





其中your_div是你网页上有一个runat =server的div



你可以得到相同的



Response.Write(xml.OuterXml)但你无法控制它在哪里渲染。


My goal is display xml string method onto the current page. I am currently using the page_load method to render the XML string but using this method, I am currently experiencing a blank web page. Is their a better approach to use, in order to solve this problem. Please advise further.

Updated Code V2:

if (!IsPostBack)
        {
          //  string a = createXMLPub(data);
         //   Label1.Text = a;
            
            createXML(data);

           // string a = createXMLPub(data);
          //  XmlDocument xml = new XmlDocument();
          //  xml.LoadXml(a);
           // Response.Write(xml.OuterXml);
          //  format_dvi.InnerText = xml.OuterXml;

          //  Response.Clear();
         //   string a = createXML(data);
         //   XmlDocument xml = new XmlDocument();
         //   xml.LoadXml(a);
         //   Response.ContentType = "text/xml";
         //   Response.Write(xml.OuterXml);
         //   Response.End();
        }
		
	public void createXML(string data)
    {
        string a = createXMLPub(data);
        XDocument xDoc = new XDocument(a);
        Label1.Text = a;
        XDocument.Parse(a);
        try
        {
            xDoc = XDocument.Load(data);
        
        }
        catch (Exception ex)
        {
            Label1.Text = ex.ToString();
            return;
        }
    }



public static string createXMLPub(string data )
    {
        XElement xeRoot = new XElement("pub");
        XElement xeName = new XElement("name", "###");
        xeRoot.Add(xeName);
        XElement xeCategory = new XElement("category", "#####");
        xeRoot.Add(xeCategory);
		XDocument xDoc = new XDocument(xeRoot);
        //xDoc.Save(Server.MapPath("products-string2.xml"));
        //Response.Redirect("products-string2.xml");//load web browser
        //return xDoc;
        data = xDoc.ToString();
        return data;
    }



Thank you

解决方案

It will display xml in current page, but will clear all other content..
Only display a XML Document

protected void Page_Load(object sender, EventArgs e)
        {
            //Clear page
            Response.Clear();
            XmlDocument xml = new XmlDocument();
            xml.Load(Server.MapPath("products-string2.xml"));
            Response.ContentType = "text/xml";
            Response.Write(xml.OuterXml);
            Response.End();
        }




Version 2

As per your update I created the same XML from here

protected void Page_Load(object sender, EventArgs e)
        {
            
            
            Response.Clear();
            string data = string.Empty;
            XElement xeRoot = new XElement("pub");
            XElement xeName = new XElement("name", "Test Name");
            xeRoot.Add(xeName);
            XElement xeCategory = new XElement("category", "Test Category");
            xeRoot.Add(xeCategory);
            XDocument xDoc = new XDocument(xeRoot);
            data = xDoc.ToString();
            
            //Clear page
            Response.ContentType = "text/xml";
            Response.Write(data);
            Response.End();
        }



Its giving me a XML

<pub>
  <name>Test Name</name>
  <category>Test Category</category>
</pub>




Version 3

I don't know what you passing to the function createXMLPub
you can also put some brakpoint to debug..

protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            string data = createXMLPub(string.Empty);
            //Clear page
            Response.ContentType = "text/xml";
            Response.Write(data);
            Response.End();
        }

        public string createXMLPub(string data)
        {
            XElement xeRoot = new XElement("pub");
            XElement xeName = new XElement("name", "Test Name");
            xeRoot.Add(xeName);
            XElement xeCategory = new XElement("category", "Test Category");
            xeRoot.Add(xeCategory);
            XDocument xDoc = new XDocument(xeRoot);
            data = xDoc.ToString();
            return data;
        }


You can use XmlTextReader. It is simple.

xml.InnerText = "";
    XmlTextReader xlRead = new XmlTextReader(Server.MapPath("library.xml"));

    while (xlRead.Read())
    {
        xlRead.MoveToElement();

        div_xml.InnerHtml = div_xml.InnerHtml + "<br />" + xlRead.Name + " " + xlRead.Value;
    }




Read XML using Asp.Net XmlTextReader


If you're trying to show whole XML (as opposed to its data) just use XmlDocument

protected void Page_Load(object sender, EventArgs e, string data)
    {
        if (!IsPostBack)
  

XmlDocument xml = new XmlDocument;
xml.LoadXml(data);
your_div.InnerText = xml.OuterXml;
      }



Where your_div is some div with runat="server" on your page

You could get the same with

Response.Write(xml.OuterXml) but you wouldn't have any control on where it would render.


这篇关于在asp.net网页上显示xml字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆