简体   繁体   中英

Format XML in Browser (asp.net, linq to xml)

Is there an easy way to write an XElement object to a browser so that it's formatted nicely, like this: http://www.4guysfromrolla.com/demos/UserInfoWriter.2.aspx ? I don't want to have to create any sort of strongly typed class, or explicitly write every node.

Example:

var xElement = new XElement("test", new XElement("node", "1"));

can I do something with xElement at this point?

Have you tried just using an XML server control and that doesnt meet your needs? It is specifically for the purpose of displaying XML on a web page.

Here is the link to MSDN

It depends on what you're trying to do, but if you change content type to text/xml the browser will display formatted XML. However, this won't work if you have other content you're trying to display on the page.

Well if you want to serialize that XElement object to the browser use eg

Response.ContentType = "application/xml";
xElement.Save(Response.OutputStream);

As others have noted, that is only useful if your ASP.NET page sends nothing but that XML document to the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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