Display xml data into html - c#

I have a xml structure stored in XDocument.
I want to present as html document (or something similar) , main idea that a web browser will be able to present it .
Does XSLT will right technology here ?
Is there some examples for how to do so ?
Thansk for help.

Yes, XSLT is good for this. I recently had to do this using the following code:
var xslt = new XslCompiledTransform(true);
xslt.Load(styleSheetFile, XsltSettings.TrustedXslt, new XmlUrlResolver());
xslt.Transform(xmlFile, outputFile);

You can use XSLT or LinqToXML. Many examples out there but you can start # http://msdn.microsoft.com/en-us/library/bb387098.aspx

Related

C# Webservice response xml displaying as pdf

I am new to C# web development. I am developing a software that receives response from webservice in XML format. (includes barcodes generated by webservice).
There is an option given by webservice provider, that i have to add a line
(Example<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">)
as a second line in the xml and display in web browser by using style sheets provided by webservice provider. If i have to choose this option, how can i add that line as second line in the received xml file also how can i map the style sheets provided by the webserive in the project for this xml.
If i dont take that option, Is it possible to display the data in xml as a pdf(includes barcodes generated by webservice), if i dont choose the option .
If I understand your question correctly, you want to:
Add a stylesheet specification to an existing XML
Convert an XML to PDF
1. ADDING A STYLESHEET
There is an option given by webservice provider, that i have to add a line [...] as a second line in the xml and display in web browser by using style sheets
This is done using e.g. Linq, like in this answer.
First of all, I think the example you used, i.e.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
may be inaccurate, as it is the first line of a XSL file (a stylesheet); those kind of files are used to transform an XML into another file (a different XML or an HTML, like in your case). However, you say
using style sheets provided by webservice provider
so my guess is that you already have those stylesheets and you can you use them, rather than creating them yourself.
If so, the line you want to add is like
<?xml-stylesheet type="text/xsl" href="helloWorld.xsl"?>
Let's suppose you already have your XML stored into an XDocument variable named "Document" with its root element being "Root"
var FilePath = "Example.xml";
var Document = XDocument.Load(FilePath);
var Root = XDocument.Descendants("Root").Single();
Then you can add your stylesheet this way, getting a new XML:
var NewDocument = new XDocument(
new XProcessingInstruction("xml-stylesheet", "type='text/xsl'ref='helloWorld.xsl'"),
Root);
2. XML to PDF
There are several ways to do this.
You might parse your XML, retrieve the elements you want to show on your PDF, use a library like iTextSharp to create a specific layout and place their contents on the file.
Or, since you already have an XML and you can transform it to an HTML using an XSL, you can use wkHtmlToPdf.
Let me know if you need more details.

scraping data from website with a C# console application

I'm trying to learn Spanish and making some flash cards (for my personal use) to help me learn the verbs.
Here is an example, page example. So near the top of the page you will see the past participle: bloqueado & gerund: bloqueando. It is these two values that I wish to obtain in my code and use for my flash cards.
If this is possible I will use a C# console application. I am aware that scraping data from a website is not ideal however this is a once off.
Any guidance on how to start something like this and pitfalls to avoid would be very helpful!
I know this isn't an exact answer, but here is the process I would suggest.
https://www.gnu.org/software/wget/ and mirror the website to a
folder. Wget is a web spider and will follow the links on the site until it has downloaded everything. You'll have to run it with a few different parameters until you figure out the correct settings you want.
Use C# to run through each file in the folder and extract the
words from <section class="verb-mood-section"> in each file. It's your choosing of whether you want to output them to the console or store them in a database or flat file.
Should be that easy, in theory.
Use SGMLReader. SGMLReader is a versatile and robust component that will stream HTML to an XMLReader:
XmlDocument FromHtml(TextReader reader) {
// setup SgmlReader
Sgml.SgmlReader sgmlReader = new Sgml.SgmlReader();
sgmlReader.DocType = "HTML";
sgmlReader.WhitespaceHandling = WhitespaceHandling.All;
sgmlReader.CaseFolding = Sgml.CaseFolding.ToLower;
sgmlReader.InputStream = reader;
// create document
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.XmlResolver = null;
doc.Load(sgmlReader);
return doc;
}
You can see that you need to create a TextReader first. TThis would in reality be a StreamReader as a TextReader is an abstract class.
Then you create the XMLDocument over that. Once you've got it into the XMLDocument you can use the various methods supported by XMLDocument to isolate and extract the nodes you need. I'll leave you to explore that aspect of it.
You might try using the XDocument class as it's a lot easier to handle than the XMLDocument, especially if you're a newbie. It also supports LINQ.

Read an XML file from http address

I need to read an xml file using c#/.net from a source like so: https://10.1.12.15/xmldata?item=all
That is basically just an xml file.
StreamReader does not like that.
What's the best way to read the contents of that link?
The file looks like so:
- <RIMP>
- <HSI>
<SBSN>CZ325000123</SBSN>
<SPN>ProLiant DL380p Gen8</SPN>
<UUID>BBBBBBGGGGHHHJJJJ</UUID>
<SP>1</SP>
<cUUID>0000-000-222-22222-333333333333</cUUID>
- <VIRTUAL>...
You'll want to use LINQ to XML to process the XML file. The XDocument.Load Method supports loading an XML document from an URI:
var document = XDocument.Load("https://10.1.12.15/xmldata?item=all");
Another way to do this is using the XmlDocument class. A lot of servers around the world are still running .Net Framework < 3.0 so it's good to know that this class still exists alongside XDocumentin case you're developing an application that will be run on a server.
string url = #"https://10.1.12.15/xmldata?item=all";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
Maybe the correct answer must starting by reading the initial question about how to "Read an XML file from a URL (or in this case from a Http address)".
I think that can be the best for you see the next easy demos:
(In this case XmlTextReader but today you can use XmlReader instead of XmlTextReader)
http://support.microsoft.com/en-us/kb/307643
(Parallel you could read this documentation too).
https://msdn.microsoft.com/en-us/library/system.xml.xmlreader(v=vs.110).aspx
regards

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.

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