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
<BR>
</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);