Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
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;
        }
    }


C#
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
Posted
Updated 9-Sep-14 5:43am
v4
Comments
Sinisa Hajnal 9-Sep-14 7:17am    
What do you mean it does not allow you to debug?!

Also, do you need to redirect or you can just show the xml on the current page. And why?
miss786 9-Sep-14 7:22am    
Thank you for your reply. I am trying to show it on the current page. Would LoadXML property work here?
[no name] 9-Sep-14 7:34am    
It's most likely not allowing you to debug it because your code will not compile.

It will display xml in current page, but will clear all other content..
Only display a XML Document
C#
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
C#
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
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..

C#
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;
        }
 
Share this answer
 
v3
Comments
miss786 9-Sep-14 11:10am    
Dear Raju, Thank you for your solution, but i am sorry to inform, I could not get your solution to work, hence I am still getting the same error. I have updated my code in the above post. My goal is to view the XML string from the CreateXMLPub() method on to the web page. Further guide, into what I could look into to solve this issue, please. Many thanks.
raju melveetilpurayil 9-Sep-14 11:28am    
Is that possible to see the xml?
miss786 9-Sep-14 11:43am    
Dear Raju, Thank you for your response. I have added CreateXMLPub() method, in the original post. Thank you for your help.
raju melveetilpurayil 9-Sep-14 12:00pm    
Thanks, Can you see XML in page source? (I mean ViewSource )
miss786 9-Sep-14 12:05pm    
Apology for not mentioning this in the original post, but there is not html data in the ViewSource page either. I have checked this many times with different code.

I am little stuck, what other approaches to take, in order to render XML string on to current page.

Many thanks for your time and help.
You can use XmlTextReader. It is simple.

C#
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
 
Share this answer
 
v2
If you're trying to show whole XML (as opposed to its data) just use XmlDocument

C#
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.
 
Share this answer
 
Comments
miss786 9-Sep-14 7:49am    
Hi, Thank you very much for your approach. I followed you guide but I am still experiencing a blank page.
string a = createXMLPub(data);
XmlDocument xml = new XmlDocument();
// xml.LoadXml(a);
// Response.Write(xml.OuterXml);
// format_dvi.InnerText = xml.OuterXml;


However, I tried creating try and catch method and calling that in the page load, however that also is showing blank page.
public void createXML(string data)
{
string a = createXMLPub(data);
XDocument xDoc = new XDocument(a);
try
{
xDoc = XDocument.Load(Server.MapPath("xml/products-string2.xml"));
// xDoc.Load(path + "products-string2.xml");
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
return;
}

Response.Redirect("products-string2.xml");//load web browser

}

Please advice further.
Sinisa Hajnal 9-Sep-14 7:53am    
Remove response.redirect, data maybe loads, but you immediately redirect to another page. It does not load web browser, it simply sends your browser to another link. Browser will show the page after Page_Load finishes without you having to call anything.
Try this:
C#
public void createXML(string data)
{
    string a = createXMLPub(data);
    Label1.Text = HttpUtility.HtmlEncode(a);
}


The reason you're getting a blank page is most likely that the browser is interpreting the XML tags as unknown HTML tags.
 
Share this answer
 
Comments
miss786 9-Sep-14 11:46am    
Dear Richard, Thank you for your solution input. I am sorry to inform, I was unable to get your approach to work. Do I need to add extra properties to the label tag in my HTML;

<form id="form1" runat="server">
<div id="format_dvi" runat="server">
<asp:label ID="Label1" runat="server" >
</div>
</form>

Thank you
Richard Deeming 9-Sep-14 11:48am    
Assuming the label tag is actually closed (either <asp:Label ... /> or <asp:Label ...></asp:Label>), there's nothing wrong with the markup.

What error do you get?
miss786 9-Sep-14 11:53am    
I am not getting no errors, just blank page. I tried to debug the page_load method, which calls the createXML(), but it does not show the local variable window, hence I am assuming my approach to call the createXML() in the page_load method, maybe incorrect.

Please advice, further if possible. Many thanks.
Richard Deeming 9-Sep-14 12:05pm    
"window" is a client-side variable, so it won't be available from server-side code.

What happens if you change the createXML method to just: Label1.Text = "createXML"; - do you still get a blank page?
miss786 9-Sep-14 12:10pm    
I see. Sadly still no luck with your above suggestion.

public void createXML(string data)
{
// string a = createXMLPub(data);
// Label1.Text = HttpUtility.HtmlEncode(a);
Label1.Text = "createXML";
}

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

createXML2(data);
}

Thanks for reply.
Dear all,

Thank you so much for contributing your solutions to this issue. I really appreciate your time and thoughtful solutions, especially Richard & Raju.

From Richard's recent suggestion, I manage to find my control label tag, was getting render as hidden, hence I used a
FindControl() method
, in my aspx page. I also found, my main method 'CreateXMLPub', was calling extra null string parameter, which I edited to the following below and was able to render the xml to my label successfully.

C#
Protected void Page_Load(object sender, EventArgs e)
        {
            string pub = createXMLPub();
            Label2.Text = Server.HtmlEncode(pub);
        }



public static string createXMLPub()
    {
        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();
        return data;
    }


Many thanks for all your help and time. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900