user222427
user222427

Reputation:

ASP - How to take an already defined XML structure and display it in HTML

Heres the scenario. I have a formatted XML structure saved in my database as nvarchar(max). I need to display this in a webpage.

I can grab the data, but how do I display it? All the examples I find require an XML file. I have the datasource.

Upvotes: 0

Views: 39

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could provide a holder for the data in your ASPX page:

<asp:Literal ID="xmlHolder" runat="server" Mode="Encode" />

and in your code behind populate it from the database:

protected void Page_Load(object sender, EventArgs e)
{
    // Fetch the XML from your database
    string xml = "<someTag>Hello</someTag>";

    // Display it on the page
    xmlHolder.Text = xml;
}

It's important to set the Mode="Encode" property on the Literal to ensure that the value is properly HYML encoded.

Upvotes: 0

WraithNath
WraithNath

Reputation: 18013

You can read an XML string with an XML document..

using System;
using System.Xml;

public class Sample {

  public static void Main() {

    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<item><name>wrench</name></item>");


    using (var stringWriter = new StringWriter())
      using (var xmlTextWriter = XmlWriter.Create(stringWriter))
      {
        xmlDoc.WriteTo(xmlTextWriter);
        xmlTextWriter.Flush();
        string s = stringWriter.GetStringBuilder().ToString();
      }
    }
}

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml(v=vs.110).aspx

once you have the xml in an xmlDocument you can toString it and print to the web page

Upvotes: 1

Related Questions