By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,077 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,077 IT Pros & Developers. It's quick & easy.

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

P: n/a
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);


Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

"anthonysmales" <an******************@nospam.com> wrote in message
news:vj************@corp.supernews.com...
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);



Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.