How to convert XML to HTML using c# dynamically - c#

How to upload XML file and generate Html file without XSLT file using C#...

LINQ to XML is one option. See also: Generating HTML using LINQ

The question is a little open-ended, but you might consider just writing a program that:
Deserializes the XML.
Having read in the XML, format the text.
Output the text using the standard in/out libraries in C#.
I don't know if this is the most efficient solution -- but I think it would work.

Related

Send Output direct to HTML or through XML first?

I am writing a parser to parse incoming text files. I have it to where it will parse everything accurately.
I have an option for it to output to text - this was done to check the accuracy of the parsing. I am currently implementing an option to write to a spreadsheet but it doesn't output everything yet.
I have a request to output as static HTML. Is it worth outputting to XML and then generating HTML from that?
I see C# has the XMLTransform class which looks like it would do what I need. Is using the XML designer in VS and writing the XSLT file easier than hand-coding all of the HTML output? I know Excel will import XML files, but it is a little messy and I don't get the formatting options I can get if I generate the .xls file directly
I would give you a qualified No.
It is generally not worth building XML then running it through an XSLT transformation to build HTML.
That said, I might consider such an option if I wanted to easily swap out transformations, such as if this is an app used by multiple clients and the generated HTML would be client dependent. Even then I'd investigate using a simple tokenized HTML template in which I just plugged in the data I wanted. However, if the transformation was sufficiently complex then, yes, I'd go the XSLT route.
The reason for the No is that by the conversion adds such a level of complexity that it is usually not worth the time involved.

Using C# to display an XML file transformed by XSLT

I don't know if this is possible or if I am thinking about this in the wrong way, but this is what I want to do:
I have an XML file linked to an XSLT file and I want to use C# to get the output of the transformed XML file and Response.Write() that wherever I want on the page.
I have found questions on stackoverflow about saving the transformed output to a new file etc, but I don't want to save it to a file, I just want to display it with Response.Write() anywhere on my aspx page.
Is there any way to do this in C#?
Any help is appreciated.
Yes, save the transformed file to a MemoryStream (so in memory not the hard disk). You can then output that to a string using a filestrem reader.
Another way of doing it is by using the XML control, it has XML and XSLT properties.
You could save yourself the effort and simply serve up the XML to the browser. As long as the XML document references the URL of the corresponding XSLT document, the browser will render the page for you.
Use HttpResponse.OutputStream as output stream to save transformed file.

How to validate an XML document?

My C#/.NET application reads XML files that are manually edited by the users. The allowed elements and tags are described in the application's documentation. I'm using LINQ to extract data from the XML file.
Before extracting data from the XML file, I'd like to validate it to see if it has the expected structure. If not, it would be nice to have information about what is wrong so that I can give some feeback to the user.
What's the simplest way to do this in C#?
You can validate xml files against XSD.
First you have to create Xml Schema Definition file. See example
use XML Schema Definition Tool to create XSD from XMLfile
Use this code to validate input XML using corresponding XSD
Hope this will help...
EDIT
This article explains all possible ways to validate xml, using C#
How To Validate an XML Document by Using DTD, XDR, or XSD in Visual C# .NET
IMO best option is to use XSD.
Validating Input Xml Data Files

Generating text file from database

I have a requirement to hand-code an text file from data residing in a SQL table. Just wondering if there are any best practices here. Should I write it as an XMLDocument first and transform using XSL or just use Streamwriter and skip transformation altogether? The generated text file will be in EDIFACT format, so layout is very specific.
The normal thing to do is just write the EDIFACT data directly.
Creating it as an XMLDocument and transforming it to EDIFACT might be useful if there's a library already available to do the transformation. I say this because there's a lot of language support for XML output.
I can't see how XSL will help you here, but I've never had to output EDIFACT data.
http://www.stylusstudio.com/edi/XML_to_EDIFACT.html
This URL has an example XSLT for translating XML to EDIFACT which might solve your problem.

Converting log files to XML and (X)HTML, recommendedations

I've been tasked with converting some text log files from a test reporting tool that I've inherited. The tool is a compiled C# (.NET 3.5) application.
I want to parse and convert a group of logically connected log files to a single XML report file, which is not a problem. The System.Xml classes are easy enough to use.
However, I also want to create a more "readable" file to accompany each report. I've chosen HTML, and because I like standardization, I'd prefer to do it in proper XHTML.
My question is how should I go about creating the HTML files along with the XML reports? My initial thought is to build the XML file, then to use LINQ and a simple StreamWriter to build an HTML file within my C# code. I could also use XSLT as opposed to LINQ to make the C# code easier. But since I have to compile this anyway, I don't like the idea of adding more files to the installation/distribution.
Is using LINQ going to cause me any problems as opposed to XSLT? Are there any nice HTML writing libraries for .NET that conform to XHTML? Since I have everything parsed from the log files in working memory, is there an easy way to create both files at the same time easily?
I'd create an xslt transform and just run that against the XML. Linq really isn't designed to transform XML of one schema (e.g., your report) to another (e.g., xhtml). You could brute force it, but xslt is an elegant way to do it.
I would actually recommend using XSL transform. Since you already have the XML doc. If you write a good XSL transform you will get very good results.
http://www.w3schools.com/xsl/xsl_transformation.asp
small snippet:
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(HttpContext.Current.Server.MapPath(xslPath));
StringBuilder sb = new StringBuilder();
using (TextWriter tw = new StringWriter(sb))
{
// Where the magic happens
xsl.Transform(xmlDoc, null, tw);
//return of text which you could save to file...
return sb.ToString();
}
One nice thing with using the XSLT is that you can put a processing instruction at the top of the XML that tells the user's browser how to generate the report itself:
<?xml-stylesheet type="text/xsl" href="Your_XSLT.xslt"?>
That way you don't have to have a separate step to generate the report. The user just opens the XML file directly in the browser, but they see the generated report instead.

Categories