XSLT, XML. HTML helper not working - c#

So I retrieve an XML and XSLT as strings from the DB then used my custom HTML helper called RenderXml to render. The problem is that it is not rendering properly. In my HTML helper is where I do the transformation
public static HtmlString RenderXml(this HtmlHelper helper, string theXml, string theXslt)
{
XDocument xmlTree = XDocument.Parse(xmlPath);
XDocument newTree = new XDocument();
using (XmlWriter writer = newTree.CreateWriter())
{
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XmlReader.Create(new StringReader(xsltPath)));
// Execute the transform and output the results to a writer.
xslt.Transform(xmlTree.CreateReader(), writer);
return new HtmlString(newTree.ToString());
}
}
in my CSHTML I do it like this
<div id="results">#Html.RenderXml(Model.theXmlFile, Model.theXSLFile,)</div>
When I try to return new HtmlString(newTree.ToString()); I get a blank, and when I try to return new HtmlString(writer.ToString()); I get this System.Xml.XmlWellFormedWriter on the page. Do anybody know what I am doing wrong? I got my code from the example here. I have also looked into this one and it works great except I do not use a URI, and when I try to modify it to use XDocument complains about missing parameters, so I stayed away from it. Any ideas why he transformation is not working?

Try moving the line return new HtmlString(newTree.ToString()); outside of the using block:
using (XmlWriter writer = newTree.CreateWriter())
{
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XmlReader.Create(new StringReader(xsltPath)));
// Execute the transform and output the results to a writer.
xslt.Transform(xmlTree.CreateReader(), writer);
}
return new HtmlString(newTree.ToString());

Related

HTML File with xml and xmln declaration cannot be transformed

I've generated a HTML file and the top html declaration looks like this:
<html xml:lang="de-CH" lang="de-CH" xmlns="http://www.w3.org/1999/xhtml">
And then I try to convert it into a different format with this .Net 4 code:
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Ignore;
XslCompiledTransform proc = new XslCompiledTransform();
proc.Load("Html_to_Sql.xslt");
fsHtmlXml = new FileStream(file.Name, FileMode.Create);
html = XmlReader.Create(file.FullName, settings);
proc.Transform(html, null, fsHtmlXml);
Unfortunately nothing happens as long as I have the xml, lang and xmlns attributes in the HTML.
Why is that?
Your XSLT will need to refer to elements in the http://www.w3.org/1999/xhtml namespace. You haven't posted your XSLT code yet, the the problem most likely lies in that file.
Will this work via XML and XPath
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
public class TransformXML
{
//This will transform xml document using xslt and produce result xml document
//and display it
public static void Main(string[] args)
{
try
{
XPathDocument myXPathDocument = new XPathDocument(sourceDoc);
XslTransform myXslTransform = new XslTransform();
XmlTextWriter writer = new XmlTextWriter(resultDoc, null);
myXslTransform.Load(xsltDoc);
myXslTransform.Transform(myXPathDocument, null, writer);
writer.Close();
StreamReader stream = new StreamReader (resultDoc);
Console.Write("**This is result document**\n\n");
Console.Write(stream.ReadToEnd());
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
}
}
The xmlns attribute specifies the namespace of the XML document. This works in much the same way as namespaces within C#, where two classes with the same name but different namespaces are considered to be completely different classes. Changing the XML namespaces means that your XSLT templates / XPath will not match.

XmlDocument in place transform with XSL

I'm sorry if this is an obvious question, but I'm getting abit frustrated trying to find an answer.
Can I perform an XSL transform on a loaded XmlDocument in place? That is, without having to create a writer to the document?
I ask because I have an XmlDocument binding inside a WPF app that I want to sort. The sorts can get a little complicated so XSL seemed a good fit. Here's the code that I'm stuck at:
XmlDataProvider xmlDP = (XmlDataProvider)this.Resources["ItemDB"];
string xsltPath = System.Configuration.ConfigurationManager.AppSettings["XSLDirextory"];
string path = xsltPath + "SortItemName.xslt";
if (System.IO.File.Exists(path))
{
XslCompiledTransform compTrans = new XslCompiledTransform();
compTrans.Load(path);
//compTrans.Transform(xmlDP.Document, new XsltArgumentList(), xmlDP.Document.XmlResolver);
}
After loading the transform, I'd like to just be able to compTrans(xmlDP.Document); or something that has the same effect. (to be clear, xmlDP.Document is an XmlDocument ) so that the XmlDocument has the result of the transform.
What's the best way to accomplish this?
The closest you can do is create a new XmlDocument with e.g.
XmlDocument result = new XmlDocument();
using (XmlWriter xw = result.CreateNavigator().AppendChild())
{
compTrans.Transform(xmlDP.Document, null, xw);
xw.Close();
}
and then assign that to your property:
xmlDP.Document = result;
Of course that requires that xmlDP.Document can be set.
XSLT always creates a new document to hold the transformation result, it never modifies the input document.

save xslt output transformation in a file

I have a "book.xml" and "book.xslt" the output has been set on text-mode, I don't want to load text file by browser because it is so heavy I need some code to save output text file in hard-drive. How I can implement this kind of transformation by c# ?
This should work:
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(#"c:\book.xslt");
xslt.Transform(#"c:\book.xml", #"c:\output.txt");
Obviously your paths will need to be updated to match your particular scenario, for example:
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(Server.MapPath("~/book.xslt"));
xslt.Transform(Server.MapPath("~/book.xml"), Server.MapPath("~/output.txt") );
This will read your XSL file from the root of the site and transform /book.xml and save it to /output.txt.
You can find out more about the System.Xml.Xsl.XslCompiledTransform class here:
System.Xml.Xsl.XslCompiledTransform
Use the System.Xml.Xsl.XslCompiledTransform class.
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(Server.MapPath("~/book.xslt"));
transform.Transform(Server.MapPath("~/book.xml"), Server.MapPath("~/output.xml"));
(Note: this assumed all the documents are stored in the root of the web application)
By using xmwwriter and xdocument like so:
using System.Data;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public void xmltest(string xmlFilePath, string xslFilePath, string outFilePath)
{
var doc = new XPathDocument(xmlFilePath);
var writer = XmlWriter.Create(outFilePath);
var transform = new XslCompiledTransform();
// The following two lines are only needed if you need scripting.
// Because of security considerations read up on that topic on MSDN first.
var settings = new XsltSettings();
settings.EnableScript = true;
transform.Load(xslFilePath,settings,null);
transform.Transform(doc, writer);
}
More info here:
http://msdn.microsoft.com/en-us/library/14689742.aspx
regards

Trying to parse xml, but xmldocument.loadxml() is trying to download?

I have a string input that i do not know whether or not is valid xml.
I think the simplest aprroach is to wrap
new XmlDocument().LoadXml(strINPUT);
In a try/catch.
The problem im facing is, sometimes strINPUT is an html file, if the header of this file contains
<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xml:lang=""en-GB"" xmlns=""http://www.w3.org/1999/xhtml"" lang=""en-GB"">
...like many do, it actually tries to make a connection to the w3.org url, which i really dont want it doing.
Anyone know if its possible to just parse the string without trying to be clever and checking external urls? Failing that is there an alternative to xmldocument?
Try the following:
XmlDocument doc = new XmlDocument();
using (var reader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings() {
ProhibitDtd = true,
ValidationType = ValidationType.None
})) {
doc.Load(reader);
}
The code creates a reader that turns off DTD processing and validation. Checking for wellformedness will still apply.
Alternatively you can use XDocument.Parse if you can switch to using XDocument instead of XmlDocument.
I am not sure about the reason behind the problem but Have you tried XDocument and XElement classes in System.Xml.Linq
XDocument document = XDocument.Load(strINPUT , LoadOptions.None);
XElement element = XElement.Load(strINPUT );
EDIT: for xml as string try following
XDocument document = XDocument.Parse(strINPUT , LoadOptions.None );
Use XmlDocument's load method to load the xml document, use XmlNodeList to get at the elements, then retrieve the data ...
try the following:
XmlDocument xmlDoc = new XmlDocument();
//use the load method to load the XML document from the specified stream.
xmlDoc.Load("myXMLDoc.xml");
//Use the method GetElementsByTagName() to get elements that match the specified name.
XmlNodeList item = xDoc.GetElementsByTagName("item");
XmlNodeList url = xDoc.GetElementsByTagName("url");
Console.WriteLine("The item is: " + item[0].InnerText));
add a try/catch block around the above code and see what you catch, modify your code to address that situation.

XslCompiledTransform and Serialization

I am trying to implement some functions that will convert one object to another with XslCompiledTransform.
I found some implementations for Serializing an object to XML string and DeSerialize the XML string to an object.
Another function does the XslCompiledTransform from object1 to obejbct2.
To generate the XSLT file i used the Altova MapForce, just loaded the XML of the serialized objects and mapped some attributes.
Now for the problems:
first I noticed that the XslCompiledTransform doesn't work with XSLT version 2.0. is there any newer functions that do work with XSLT 2.0? maybe some settings?
secondly I get an exception when trying to DeSerialize the XML to an object:
"There was an error deserializing the object of type myObject Input string was not in a correct format."
I don't understand where is the problem.
Does anybody have a sample code that does such a thing? all I find in google are Transformations of HTML code and not objects.
Here are the functions:
private static string runXSLT(string xsltFile, string inputXML)
{
XmlDocument XmlDoc = new XmlDocument();
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform(true);
xslt.Load(xsltFile);
StringReader StrReader = new StringReader(inputXML);
XmlTextReader XmlReader = new XmlTextReader(StrReader);
//Create an XmlTextWriter which outputs to memory stream
Stream stream = new MemoryStream();
XmlWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
// Execute the transform and output the results to a file.
xslt.Transform(XmlReader, writer);
stream.Position = 0;
XmlDoc.Load(stream);
return XmlDoc.InnerXml;
}
public static string SerializeAnObject(object AnObject)
{
XmlDocument XmlDoc = new XmlDocument();
DataContractSerializer xmlDataContractSerializer = new DataContractSerializer(AnObject.GetType());
MemoryStream MemStream = new MemoryStream();
try
{
xmlDataContractSerializer.WriteObject(MemStream, AnObject);
MemStream.Position = 0;
XmlDoc.Load(MemStream);
return XmlDoc.InnerXml;
}
finally
{
MemStream.Close();
}
}
public static Object DeSerializeAnObject(string XmlOfAnObject, Type ObjectType)
{
StringReader StrReader = new StringReader(XmlOfAnObject);
DataContractSerializer xmlDataContractSerializer = new DataContractSerializer(ObjectType);
XmlTextReader XmlReader = new XmlTextReader(StrReader);
try
{
Object AnObject = xmlDataContractSerializer.ReadObject(XmlReader);
return AnObject;
}
finally
{
XmlReader.Close();
StrReader.Close();
}
}
Thanks allot,
Omri.
XslCompiledTransform does not support XSLT 2.0. In fact, XSLT 2.0 is not supported within the .NET Framework at all (you could try the Saxon version for .NET, but be aware that this is just the Java version running inside IKVM).
From your description I did not understand why you are taking the detour via XML to convert one object into another. Why don't you simply provide a constructor in your target object that takes your input object as a paramater? Then you can code all the mapping inside that constructor. This is not onlyby far more efficient than serializing, transforming and deserializing your objects you will also get the type safety of C#.

Categories

Resources