Compile error CS1647 when opening large aspx file in browser - c#

I am using XML/XSLT to create an aspx page which can grow quite large. Even though the file is successfully created, when the file size approaches 300K, the error: “CS1647: An expression is too long or complex to compile” is issued when opening the file in a browser (tried both IE and Chrome). No other useful information is shown on the failed .NET error page.
My development environment is VS 2012 Express for Web on a Win7 x64 laptop.
Since this problem does not occur during the execution of the program, I am at a loss as to how to approach solving this problem. Can anyone suggest a strategy or work around to this issue?
EDIT
The C# code used to create the aspx page is
// load the xml file
XmlDocument reportDetails = new XmlDocument();
reportDetails.Load(ReportDetailsPath);
//setup the xslt transform
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XlsRptPath);
StringWriter sw = new StringWriter();
xslt.Transform(ReportDetails, null, sw);
//create the aspx file
using (StreamWriter outfile = new StreamWriter(aspxPath))
{
outfile.Write(sw.ToString());
}

Old question, and largely answered in the comments. But for the sake of completeness, let's answer it ;). Your issue is not with XSLT itself, but with the generated file which hits the 300k boundary of maximum expression size in C#. There are a couple of things you can do:
Make the generated code below 300k (but this might not be possible in your case)
Take any string constants in the generated code and place them in a
resource file. The format of such resource files is XML and you can
auto-generate it, just make sure the IDs in the resource match the
IDs you use in the generated ASPX
Place part of the generated code in a code-behind file. An ASPX is
parsed as one expression, but the code-behind is not. This is a
matter of structuring the generated code.
Split the ASPX in multiple pages, if your design allows this. You can
recombine them with iframes.
Split the ASPX file in multiple ASCX controls. This is perhaps the
most natural thing to do. Each ASCX control can be referenced/added
to the ASPX file. Each ASCX control should not exceed the 300k limit.
If there is a lot of generated CSS that blows up the size, place it
in a separate CSS file.
If there are a lot of long absolute paths in image references and the
like, you can collapse them and make the references relative, i.e. by
using <base>, which may save you some space.
If the error is caused by actual large (constant) expressions, consider the
hints in the answers in this post for a resolution.

I am sorry for not posting my solution sooner, but I was too stressed at the time to do so. Better late than never I guess.
Instead of trying to create a complete aspx web page for each associated xml file, I created a stub and applied the xslt transform at run-time from within an associated Site.Master. The stub's MasterPageFile property is set to this Site.Master. This approach does sacrifice some performance, but it works for any size web page. Here is an example of the outputted webpage.
Example aspx stub file:
<%# Page Title="Top Austin Beauty Salons List" MetaDescription="List of best Google-ranked Austin beauty salon" Language="C#" MasterPageFile="~/Site1.Master" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
Site.Master Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
string vp = Page.AppRelativeVirtualPath;
if (vp.ToLower().EndsWith("default.aspx") || vp.ToLower().EndsWith("webform2.aspx")) return; // ignore some aspx files used for ohter reasons
string xmlPath = Page.MapPath(vp.Substring(0, vp.LastIndexOf(".")) + #".xml");
string xslPath = Page.MapPath("mainpage.xslt");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("xmlPath", "", xmlPath);
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xslPath);
// Execute the transform and output the results to a string writer.
StringWriter sw = new StringWriter();
xslt.Transform(xmlDoc, argsList, sw);
content.InnerHtml = sw.ToString(); // add the generated html to the associated stub aspx content section
}

Related

Webclient.DownloadString() does not retrieve current whole page

I know there is another question with practically identical title here: Webclient.DownloadString does not retrieve the whole page
But the solution doesn't help me, maybe somebody else have the same problem.
I'm trying to get the html code of this URL:
https://cubebrush.co/?freebies=true
To achieve that, I'm using the following code in C#:
WebClient webClient = new WebClient();
string webString = webClient.DownloadString("https://cubebrush.co/?freebies=true");
But the retrieved html lacks some information, for example, all the button tags inside the website. This can be quickly checked using the library HtmlAgilityPack and checking for all the tags inside the website with the following code:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(webString);
HashSet<string> hs = new HashSet<string>();
foreach (var dec in doc.DocumentNode.Descendants())
{
hs.Add(dec.Name);
}
If we run this, it will show 26 tags, but none of them will be a button tag. This makes sense, since the initial webString also lacks that "button information".
I've tried to copy webString into a file, to check if, as the initial commented post says, was a problem with the visualizer, but it doesn't, visualizer and file looks exactly equal.
Can somebody tells me what I'm doing wrong? Thanks!

C# - create and write HTML file with variables

Basiclly I'm trying to create an HTML, I already have it written but I want the user to be able to put some text on the textboxes and saving it into strings and use later when creating the HTML file.
I tried playing abit with StreamWriter but I don't think that will be the best idea.
Also I want it to open on the default web browser , or just on IE if it's easier after the file is created.
I really need help as I'm struggling especially with the creating part.
Thanks for reading!
You can also do this without external libraries.
Set up your HTML file as follows:
<!DOCTYPE html>
<html>
<header>
<title>{MY_TITLE}</title>
</header>
<body></body>
</html>
Then edit and save the HTML from C#:
const string fileName = "Foobar.html";
//Read HTML from file
var content = File.ReadAllText(fileName);
//Replace all values in the HTML
content = content.Replace("{MY_TITLE}", titleTextBox.Text);
//Write new HTML string to file
File.WriteAllText(fileName, content);
//Show it in the default application for handling .html files
Process.Start(fileName);
If you already have the HTML you want to export (just not customized), you could manually add format strings to it (like {0}, {1}, {2}) where you want to substitute text from your app, then embed it as a resource, load it in at runtime, substitute the TextBox text using string.Format, and finally write it out again. This is admittedly a really fragile way to do it, as you need to make sure the number of parameters agrees between the resource file and your call to string.Format. In fact, this is a horrible way to do it. Actually, you should do it the way #EmilePels suggests, which is basically a less fragile version of this answer.

No stylesheet was loaded

I am using C# code in aspx pages to covert the infopath xml pages into html. Here is my code:
XPathDocument myDoc = new XPathDocument(#"C:\Users\rameshgandhik\Documents\infopath forms\ram.xml");
XmlTextWriter myWr = new XmlTextWriter(#"C:\Users\rameshgandhik\Documents\infopath forms\ram.html",null);
XslTransform myXsl = new XslTransform();
myXsl.Transform(myDoc, null, myWr); // Here i am getting an error.
At myWr in Transform method it is showing an error that
"No stylesheet was loaded.".
Can any have the idea about this error......... Please tell me the solution....
That would be because you haven't loaded the stylesheet. :-)
You've created a new XslTransform object, but you didn't actually put any transformation rules into it. Therefore, it doesn't know how to transform the XML you're giving it, which is pretty clearly expressed in the error message.
If you want to take the transform from an *.xsl file, you can use the XslTransform.Load method.
If you want to get the transform from some other location, please specify what that location is, and I would probably be able to help you.

Creating XML in C# for jQuery

I'm trying to generate some XML for a jQuery.get (AJAX) call, and I'm getting the following error from my C# page: "Using themed css files requires a header control on the page. (e.g. <head runat="server" />)."
The file generating the XML is a simple .aspx file, consisting entirely of:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ChangePeopleService.aspx.cs" Inherits="ChangeRegister.Person.ChangePeopleService" EnableTheming="false" %>
with codebehind using Linq-to-XML, which is working ok:
XElement xml = new XElement("People",
from p in People
select new XElement("Person", new XAttribute("Id", p.Id),
new XElement("FirstName", p.FirstName)));
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Write(xml.ToString());
I know that the error relates to the Web.Config's <pages styleSheetTheme="default" theme="default"> tag, because when I remove the 'styleSheetTheme' and 'theme' attributes, the XML gets generated ok. The problem then obviously is that every other page loses its styling. All this leads me to think that I'm approaching this wrong.
My question is: what's an accepted way to generate XML in C#, for consumption by a jQuery AJAX call, say?
If I am returning simple data (not a page), I probably wouldn't use aspx; that is really web-forms, but what you are returning isn't a web-form. Two options leap to mind:
use ASP.NET MVC; sounds corny, but it really is geared up to return different types of response much more elegantly
use a handler (ashx) - which omits all the web-form noise, just leaving you with a HttpContext with which to construct your response
You could also try (within aspx) clearing the response (Clear()?) and calling Close() afterwards. But IMO a lot more roundabout than just using a handler.
You need to use theme=""
example:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ChangePeopleService.aspx.cs" Inherits="ChangeRegister.Person.ChangePeopleService" Theme="" %>
Try writing to the Response.OutputStream instead:
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
using (TextWriter textWriter
= new StreamWriter(HttpContext.Current.Response.OutputStream, Encoding.UTF8))
{
XmlTextWriter writer = new XmlTextWriter(textWriter);
writer.WriteString(xml.ToString());
}

XSLT and XML Question

I have this kinda interesting requirement.
Typically you use XSLT to transform an XML document. The transformed HTML is viewable in a web browser, this works just great. I am also guessing the browser handles the transformation in memory, because if you view the page source of an xml document with XSLT, you don't see html, only xml.
What I would like to do is the following.
using c#
grab an xml file from the fileSystem....
Load it into some framework object
attach an XSLT stylesheet
output the rendered HTML back to an html file on the file system.
Is this possible.
I don't expect a full answer on the entire solution. Just a push in the right direction would be great :) thanks in advance.
You can use System.Xml.Xsl to do XSLT in C#.
There's an article here: XML transformation using Xslt in C# that explains how - here's the core of it:
XPathDocument myXPathDoc = new XPathDocument(<xml file path>);
XslTransform myXslTrans = new XslTransform();
myXslTrans.Load(<xsl file path>);
XmlTextWriter myWriter = new XmlTextWriter("result.html", null);
myXslTrans.Transform(myXPathDoc, null, myWriter);
(Edit: Note to #John: that code illustrates the basic idea. It doesn't pretend to be production quality.)
So, I found the answer, and pretty quickly... Its all explained here...
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63
what if the html is a invaild format xml?
it looks like we can not use xslt?
Any feedbacks?

Categories

Resources