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.
Related
I'm currently working with the new German ZUGFeRD files. These are PDF A/3 files who have an embedded XML file in them which contains data.
I want to extract this XML file from the PDF A/3 using abcpdf 8.1 with C#.
Any idea how to do this ?
Thanks a lot and regards,
I don't know abcpdf but I guess that the pdf libs offer similar access to the pdfs content.
First take a look at Das-ZUGFeRD-Format_1p0.pdf. Especially page 112. The images shows the object tree you have to traverse in order to find the xml stream.
With this tree you have the names, the types and the direction. Now you can traverse the pdf object tree to get to the XML content that you are looking for.
The steps based on the diagram.
Read your PDF
Get the catalog inside your PDF
Get the Array with name AF from Catalog
Get first element from AF array (should be file spec)
From file spec get the dictionary named EF
Get the stream content of EF
This are the steps you need to perform in order to get to the content.
To display the structure of a pdf and browse the tree I would recommend to use a tool like iText RUPS
What did i do with abcpdf:
Get the Objectsoup Array from the Doc (Pretty much an array of all Objects in the Doc)
as ZUGFeRD allows only one embedded file inside the PDF, i just searched this objectsoup-array for the one of the type StreamObject that contains /EmbeddedFile
Decompress the Stream of that object, get the byte[] of the stream and write it into an xml file
I'm looking for a way to save a table from an html page as xml or json. The current method i'm using save the entire page as and xls sheet and then reads the sheet using Office.InterLop.Excel. I want to skip saving the file and just read directly from the page using HttpRequest. Any ideas?
I assume you mean that you'd like to scrape the contents of a web page without File-> Save As?
Code project has a writeup explaining using HttpWebRequest to do just that. Or, you could use the newer HttpClient. Once you retrieve the HTML, you'll have to parse it yourself.
In the MSDN artticle, they're actually requesting JSON directly, so they don't have to deal with parsing, but you could very easily write up a RegularExpression to capture the table body.
I am exporting some data from my database in xml format.
The file exported has an extension as .xml and it is viewed as Excel file.
I want to insert an image in this xml file so that when we view it as Excel we will be able to see the image along with the data.
Whatever I have found from the internet is that there is no straight forward way to insert an image in xml file as xml file were designed for handling the data.
Can any one tell me what is the approach I will have to follow in order to obtain the desired functionality.
xml files are text files, and you can embedd binary data if you encode it with Base64 algorithm.
But to view the image you will need to decode the Base64-string and pass the result binary data to an image viewer implementation.
It cannot be done in MS Excel. You will need to implement your own viewer.
You won't be able to view an image inside of an XML file unless you write a dedicated application for it that knows about your particular requirement.
The reason is that XML is character-based, but images are not - they are binary data. A way to embed your image nevertheless is to transform it to something character-based first. For example, you could Base64-encode your image first and embed the resulting string in your XML. But I suppose there is no way to tell Excel to interpret this data as an image right away.
Because embedding binary files into XML using Base64 is such a common idiom, XML Schema even has its own data type for this: base64Binary.
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.
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.