HtmlAgilityPack Erorr while open xml file - c#

I think my problem is simple, but i can't undertand it.
I have a xml file, looks like this:
<categorys>
<category name="">
<link>some_link</link>
</category>
</categorys>
and i open it like so:
var doc = new HtmlDocument();
doc.Load("categorys.xml");
HtmlDocument class read this xml like <link/>some_link. What's wrong?
For example, my file here: https://yadi.sk/d/ftS2_daS3LNHdy

As the name says, HTMLAgilityPack is meant for reading HTML documents, not XML documents (by the chance some XML documents may work if it is mostly html like).
You need to use XmlDocument class to handle that.

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.

Display deserialized XML on ASP.NET page

How can I display XML output on ASP.NET page with XML tags?
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonOutput, "root");
Console.WriteLine(doc.OuterXml);
I would like to get results on my page like this:
<root>
<id>108013515952807</id>
<posts>
<data>
<message>This...Game... http://www.youtube.com/watch?v=l8Xsex0pqXY</message>
<id>108013515952807_470604423027046</id>
<created_time>2013-05-15T20:02:31+0000</created_time>
</data>
<data>
<message>Streaming in a few minutes! http://www.youtube.com/watch?v=IYnHDT6V82k</message>
<id>108013515952807_470538076367014</id>
<created_time>2013-05-15T16:46:36+0000</created_time>
</data>
</posts>
</root>
I tried this but I get no XML tags like in example above.
Response.Write("<BR>" + doc.OuterXml);
If you just put XML onto a webpage, the browser thinks it might be HTML and "renders" it, which is why you can't see the tags. You need to encode the XML
You can use the method
Response.Write(Server.HtmlEncode(doc.OuterXml));
Try
XElement.Parse(request.OuterXml).ToString()
If you want prettified XML string,
refer to: What is the simplest way to get indented XML with line breaks from XmlDocument?

load xml file into memory

I need to load a xml file in memory so I can access it several times from different forms.
The xml is in this format:
<Slides>
<Slide>
<Name>Name 1</Name>
<Value>Value 1</Value>
<Type>Type 1</Type>
</Slide>
<Slide>
<Name>Name 2</Name>
<Value>Value 2</Value>
<Type>Type 2</Type>
</Slide>
</Slides>
I don't want to use a database to store the vars. Is there any other method to store the data in memory?
Right now I'm using a txt file for each slide and streamreader, which I'm sure is not the best option.
EDIT:
I added this code, but will I be able to get the slides every time without reading the xml file again?
var slides = from s in XElement.Load("slides.xml").Elements("Slide")
select s;
foreach (var slide in slides)
{
//code
}
You should use the XDocument Load method: http://msdn.microsoft.com/en-us/library/bb343181.aspx
i think it is better to use XDocument than the XmlDocument... but it depends on your requirements...
The XDocument is more memory optimized than the XmlDocument, and i think they both are easy for use (for getting values from the xml)
See the XmlDocument class. The Load() method does what you want.
First you have to save the xml file in your project file or your hard disk.Then only you can load the xml file in code behind.
You can create xml file dynamically.it will be much better.
using System.Xml;
XmlDocument myxml = new XmlDocument();
myxml.Load("D:/sample.xml");//Load you xml file from you disk what you want to load
string element_1 = myxml.GetElementsByTagName("Name")[0].InnerText;
string element_2 = myxml.GetElementsByTagName("Value")[0].InnerText;
string element_3 = myxml.GetElementsByTagName("Value")[0].InnerText;
string element_4 = myxml.GetElementsByTagName("Name")[1].InnerText;
string element_5 = myxml.GetElementsByTagName("Value")[1].InnerText;
string element_6 = myxml.GetElementsByTagName("Value")[1].InnerText;
Try this program it will help you.

Display xml data into html

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

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