How to get the output of an XslCompiledTransform into an XmlReader? - c#

I have an XslCompiledTransform object, and I want the output in an XmlReader object, as I need to pass it through a second stylesheet. I'm getting a bit confused - I can successfully transform some XML and read it using either a StreamReader or an XmlDocument, but when I try an XmlReader, I get nothing.
In the example below, stylesheet is my XslCompiledTransform object. The first two Console.WriteLine calls output the correct transformed XML, but the third call gives no XML. I'm guessing it might be that the XmlTextReader is expecting text, so maybe I need to wrap this in a StreamReader..? What am I doing wrong?
MemoryStream transformed = new MemoryStream();
stylesheet.Transform(input, args, transformed);
transformed.Position = 0;
StreamReader s = new StreamReader(transformed);
Console.WriteLine("s = " + s.ReadToEnd()); // writes XML
transformed.Position = 0;
XmlDocument doc = new XmlDocument();
doc.Load(transformed);
Console.WriteLine("doc = " + doc.OuterXml); // writes XML
transformed.Position = 0;
XmlReader reader = new XmlTextReader(transformed);
Console.WriteLine("reader = " + reader.ReadOuterXml()); // no XML written

The XmlReader.ReadOuterXml method reads the XML for the current node. When you first create the reader, there is no current node, so ReadOuterXml will return nothing.
If you add the line:
reader.Read();
...before the ReadOuterXml() call, then it will work as you expect.
P.S. You should normally test the result of the Read() method to ensure that the reader actually has something to read.

Related

XmlDocument not preserving whitespace

XmlDocument is adding a space at the end of self closing tags, even with PreserveWhitespace set to true.
// This fails
string originalXml = "<sample><node id=\"99\"/></sample>";
// Convert to XML
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.LoadXml(originalXml);
// Save back to a string
string extractedXml = null;
using (MemoryStream stream = new MemoryStream())
{
doc.Save(stream);
stream.Position = 0;
using(StreamReader reader = new StreamReader(stream))
{
extractedXml = reader.ReadToEnd();
}
}
// Confirm that they are identical
Assert.AreEqual(originalXml, extractedXml);
The desired output is:
<sample><node id="99"/></sample>
But I am getting:
<sample><node id="99" /></sample>
Is there a way to suppress that extra space?
Here's how XmlDocument.Save(Stream) looks like :
public virtual void Save(Stream outStream)
{
XmlDOMTextWriter xmlDomTextWriter = new XmlDOMTextWriter(outStream, this.TextEncoding);
if (!this.preserveWhitespace)
xmlDomTextWriter.Formatting = Formatting.Indented;
this.WriteTo((XmlWriter) xmlDomTextWriter);
xmlDomTextWriter.Flush();
}
So setting PreserveWhiteSpace has no effect on the inside of the nodes. The documentation of the XmlTextWriter says :
When writing an empty element, an additional space is added between tag name and the closing tag, for example . This provides compatibility with older browsers.
So I guess there is no easy way out. Here's a workaround tho:
So I wrote a wrapper class MtxXmlWriter that is derived from XmlWriter and wraps the original XmlWriter returned by XmlWriter.Create() and does all the necessary tricks.
Instead of using XmlWriter.Create() you just call one of the MtxXmlWriter.Create() methods, that's all. All other methods are directly handed over to the encapsulated original XmlWriter except for WriteEndElement(). After calling WriteEndElement() of the encapsulated XmlWriter, " />" is replaced with "/>" in the buffer:

Can't successfully clone XMLReader (Unexpected EOF)

I have a really strange problem with XMLReader/XMLTextReader classes.
I have a simple file load:
public void First()
{
XmlTextReader reader = new XmlTextReader(#"C:\MyXMLFile.xml");
XmlReader readerToSerialize;
XmlReader readerToLoad;
DuplicateReaders(reader, out readerToSerialize, out readerToLoad);
XmlSerializer serializer = new XmlSerializer(typeof(XMLTree));
XmlFeed = (XMLDescriptor)serializer.Deserialize(readerToSerialize);
xmlDoc.Load(readerToLoad);
}
protected void DuplicateReaders(XmlTextReader xmlReader, out XmlReader cloneOne, out readerToLoad)
{
XmlDocument _XmlDocument = new XmlDocument();
MemoryStream _Stream = new MemoryStream();
_XmlDocument.Load((XmlTextReader)xmlReader);
_XmlDocument.Save(_Stream);
_Stream.Position = 0L;
cloneOne = XmlReader.Create(_Stream);
_Stream.Position = 0L;
cloneTwo = XmlReader.Create(_Stream);
}
The problem is that only one of the cloned elements read the whole file successully, the next one (xmlDoc.Load) fails always at the same place (Line 91, Character 37 with this xml file). If I directly assign to xmlDoc (i.e. clone the original element only once and asign it directly from the function):
public void First()
{
XmlTextReader reader = new XmlTextReader(#"C:\MyXMLFile.xml");
XmlReader readerToSerialize;
DuplicateReaders(reader, out readerToSerialize);
XmlSerializer serializer = new XmlSerializer(typeof(XMLTree));
XmlFeed = (XMLDescriptor)serializer.Deserialize(readerToSerialize);
}
protected void DuplicateReaders(XmlTextReader xmlReader, out XmlReader cloneOne)
{
XmlDocument _XmlDocument = new XmlDocument();
MemoryStream _Stream = new MemoryStream();
_XmlDocument.Load((XmlTextReader)xmlReader);
_XmlDocument.Save(_Stream);
_Stream.Position = 0L;
cloneOne = XmlReader.Create(_Stream);
_Stream.Position = 0L;
this.xmlDoc.Load(_Stream);
}
I still get the same error 91/37 (Unexpected EOF), but this time in the Serializer.
My initial problem was that if I use xmlDoc.Load(reader) the reader instance get destroyed and I can't serialize it later on. I found the Duplicate function on the MSDN forums, but it's still a no go. What I want to achieve is quite simple:
Use only one reader and get one XmlDocument and one Serialized Class. How hard can it be?
You need to close the first reader before you can use the duplicate.
reader.Close()
Your both cloneOne and cloneTwo use the same underlying memory stream.
use a different MemoryStream
cloneTwo = XmlReader.Create(new MemoryStream(_Stream.ToArray()));
Found much easier solution, instead of cloning the two readers, i just use create a second one from XmlDoc and use it to deserialize.

Generate XML and HTML from MemoryStream

Need to generate an html report from XML and corresponding XSL butI have to use memorystream instead of IO File write on server directories. For the most part I managed to create an xml
MemoryStream ms = new MemoryStream();
XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Indent = true;
using(XmlWriter writer = XmlWriter.Create(ms,wSettings))
{
/**
creating xml here
**/
writer.Flush();
writer.Close();
}
return ms; // returning the memory stream to another function
// to create html
// This Function creates
protected string ConvertToHtml(MemoryStream xmlOutput)
{
XPathDocument document = new XPathDocument(xmlOutput);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlOutput);
StringWriter writer = new StringWriter();
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(reportDir + "MyXslFile.xsl");
transform.Transform(xDoc, null, writer);
xmlOutput.Position = 1;
StreamReader sr = new StreamReader(xmlOutput);
return sr.RearToEnd();
}
Somewhere along the line I am messing up with creating the HTML Report and cant figure out how to send that file to client end. I dont have much experience working with memorystream. So, any help would be greatly appreciated. Thank you.
You're completely bypassing your transform here:
// This Function creates
protected string ConvertToHtml(MemoryStream xmlOutput)
{
XPathDocument document = new XPathDocument(xmlOutput);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlOutput);
StringWriter writer = new StringWriter();
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(reportDir + "MyXslFile.xsl");
transform.Transform(xDoc, null, writer);
// These lines are the problem
//xmlOutput.Position = 1;
//StreamReader sr = new StreamReader(xmlOutput);
//return sr.RearToEnd();
return writer.ToString()
}
Also, calling Flush right before you call Close on a writer is redundant as Close implies a flush operation.
It is not clear to me what you want to achieve but using both XmlDocument and XPathDocument to load from the same memory stream does not make sense I think. And I would set the MemoryStream to Position 0 before loading from it so either have the function creating and writing to the memory stream ensure that it sets the Position to zero or do that before you call Load on the XmlDocument or before you create an XPathDocument, depending on what input tree model you want to use.

How to read XML document and display the output as string in c#

consider my source file looks like this.
<Content xmlns="uuid:4522eb85-0a47-45f9-8e2b-1x82c78xx920">
<first>Hello World.This is Fisrt field</first>
<second>Hello World.This is second field</second>
</Content>
I want to write a code, which read this xml document from a location and display it as string.
say name of the xml file is helloworld.xml.
Location: D:\abcd\cdef\all\helloworld.xml.
I have tried the following, but i was unable to do it.
XmlDocument contentxml = new XmlDocument();
contentxml.LoadXml(#"D:\abcd\cdef\all\helloworld.xml");
Response.Write("<BR>" + contentxml.ToString());
Response.write is displaying nothing. Correct me if i missed any thing. Its not creating any component and error is coming.
I have also tried this,
XmlDocument contentxml = new XmlDocument();
try
{
contentxml.LoadXml(#"D:\abcd\cdef\all\helloworld.xml");
}
catch (XmlException exp)
{
Console.WriteLine(exp.Message);
}
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
contentxml.WriteTo(xw);
Response.Write("<BR>" + sw.ToString());
But i did not find the any output.
I want to read a XML file from a location and display it as it is as string.
Can anyone help on this.
Thank you,
Muzimil.
You need the OuterXml property:
Response.Write("<BR>" + contentxml.OuterXml);
Also you are loading a file not xml so use
contentxml.Load(#"D:\abcd\cdef\all\helloworld.xml");
instead of
contentxml.LoadXml(#"D:\abcd\cdef\all\helloworld.xml");
Do you really have to deserialize the XML at all? Why not just read it as a text file? Something like..
String text = File.ReadAllText(#"D:\abcd\cdef\all\helloworld.xml");
Response.Write(text);
With appropriate error handling obviously..
I would try using the XDocument class:
//load the document from file
var doc = XDocument.Load("..."); //== path to the file
//write the xml to the screen
Response.Write(doc.ToString());
If you want to use an XmlDocument instead, you would want to use Load instead LoadXml.
If you want to simply write a file to the output, you can do Response.WriteFile.
try this
XmlTextReader reader = new XmlTextReader (#"D:\abcd\cdef\all\helloworld.xml");
while (reader.Read())
{
Console.WriteLine(reader.Name);
}
Console.ReadLine();
String text = File.ReadAllText(Server.MapPath("~/App_Data/sample.xml"));
txtData.Text = text;

Apply XSLT on in-memory XML and returning in-memory XML

I am looking for a static function in the .NET framework which takes an XML snippet and an XSLT file, applies the transformation in memory, and returns the transformed XML.
I would like to do this:
string rawXml = invoiceTemplateDoc.MainDocumentPart.Document.InnerXml;
rawXml = DoXsltTransformation(rawXml, #"c:\prepare-invoice.xslt"));
// ... do more manipulations on the rawXml
Alternatively, instead of taking and returning strings, it could be taking and returning XmlNodes.
Is there such a function?
You can use the StringReader and StringWriter classes :
string input = "<?xml version=\"1.0\"?> ...";
string output;
using (StringReader sReader = new StringReader(input))
using (XmlReader xReader = XmlReader.Create(sReader))
using (StringWriter sWriter = new StringWriter())
using (XmlWriter xWriter = XmlWriter.Create(sWriter))
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("transform.xsl");
xslt.Transform(xReader, xWriter);
output = sWriter.ToString();
}
A little know feature is that you can in fact transform data directly into a XmlDocument DOM or into a LINQ-to-XML XElement or XDocument (via the CreateWriter() method) without having to pass through a text form by getting an XmlWriter instance to feed them with data.
Assuming that your XML input is IXPathNavigable and that you have loaded a XslCompiledTransform instance, you can do the following:
XmlDocument target = new XmlDocument(input.CreateNavigator().NameTable);
using (XmlWriter writer = target.CreateNavigator().AppendChild()) {
transform.Transform(input, writer);
}
You then have the transformed document in the taget document. Note that there are other overloads on the transform to allow you to pass XSLT arguments and extensions into the stylesheet.
If you want you can write your own static helper or extension method to perform the transform as you need it. However, it may be a good idea to cache the transform since loading and compiling it is not free.
Have you noticed that there is the XsltCompiledTransform class?

Categories