How to wrap HTML in XML and send it to a webcontrol??

A

anthonysmales

I have a webservice which implements a webmethod called PrettyXML. This
webmethod loads an XML file and returns the XML, which is then displayed in
a webcontrol (textbox) on a webform.

If no XML file is found, it just returns null atm. I would now like to alter
it so that if the XML file is not found, some HTML is sent to the webcontrol
instead. The problem with this is that I cannot return Html because the
public method is XmlDocument, so I will have to wrap the Html inside of an
Xml tag for it to be able to return the data.

I need it to output something like this (so that the tags get encoded too):

<content>
this is html
&lt;BR&gt;
</content>

Here is my code so far:

[WebMethod()]
public XmlDocument getPrettyXML(string aFilename)
{

if (aFilename != null) {
FileInfo aFile = new FileInfo(Server.MapPath(aFilename));

if (aFile.Exists) {
// file exists - output XML
StringBuilder sb = new StringBuilder((int) aFile.Length*2);
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(aFilename));
return doc;

} else {

// file does not exist - output some html

StringBuilder sb = new StringBuilder((int) 1000);

HtmlTextWriter writer = new HtmlTextWriter (new StringWriter(sb));

writer.Write("this is html");
writer.WriteLine();

// here I need to wrap the Html in Xml and return it

return sb.ToString(); // can't do this because the public
method is XmlDocument
}

}else{

return null;

}
}







StringBuilder sb = new StringBuilder();

WebUserControl1 myPortlet = new WebUserControl1();

StringWriter myStringWriter = new StringWriter(sb);

HtmlTextWriter myHtmlTextWriter = new
HtmlTextWriter((TextWriter)myStringWriter);

myPortlet.RenderControl(myHtmlTextWriter);
 
C

codewriter

You can create a new XML document without loading from file, use new nodes.
Populate them in your code at run tyme. Or better off, you can use a special
XML template that your XmlDoc will read in case file does not exist,
somethig like ErrorHTML.xml. this one you can build using XML editor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top