364,085 Members | 5185 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Transform XML string using XSL file (v1.1)

Andrew Connell
P: n/a
Andrew Connell
Having fits transforming an XML string using an XSL file. In the 1.1
version of the framework, I see that the XmlResolver is heavily used
in the XslTransform class. However, that looks like I am only
supposed to use that only when you have an xml ~file~... not an XML
string (my XML is coming from a database field).

Is there a cut & dry example out there on how to transform a simple
XML string using XSL?

-AC
Nov 12 '05 #1
Share this Question
Share on Google+
5 Replies


Jim Hughes
P: n/a
Jim Hughes
I believe that you can use Nothing for the XMLResolver parameter in VB.Net
or null in C# if you so choose, thereby not enforcing the Security
enhancements.

ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconxsltransformclassimpleme
ntsxsltprocessor.htm

"Andrew Connell" <spam@aconnell.com> wrote in message
news:245e70a2.0311231205.312661ae@posting.google.c om...[color=blue]
> Having fits transforming an XML string using an XSL file. In the 1.1
> version of the framework, I see that the XmlResolver is heavily used
> in the XslTransform class. However, that looks like I am only
> supposed to use that only when you have an xml ~file~... not an XML
> string (my XML is coming from a database field).
>
> Is there a cut & dry example out there on how to transform a simple
> XML string using XSL?
>
> -AC[/color]


Nov 12 '05 #2

gaustin@proposalheaven.com
P: n/a
gaustin@proposalheaven.com
Snap - am having the same fits.

Having sorted out much of the erroneous information there is with folk confusing 1.0 and 1.1 framework. No great progress but.

I am trying to mereg db contents and present formatted on screen

Using a xml web control and this

OleDbDataAdapter1.Fill(DataSet11) '
str = DataSet11.GetXml
' Xml1.TransformSource("XSLTFile1.xslt")
Xml1.DocumentContent = str

I get a string of xml data with no formatting! the elements are concatenated

If I save the xml string and use the file i get same result.

If i remove from the file the header

<?xml version="1.0" standalone="yes"?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd"> whihs is generated in the GetXML function it works. I get xml formatted acording to the XSLT.

Am persisting and will share resutls, look forward to any info you have. Good luck!




************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 12 '05 #3

Anders Borum
P: n/a
Anders Borum
Hello!
[color=blue]
> I get a string of xml data with no formatting!
> the elements are concatenated[/color]

Just create an XmlTextWriter with indenting as formatting. Then run your
unformatted string through the writer and voila, a perfectly formatting Xml
document as string. This is also called pretty-printing!

/// <summary>
/// Formats the string Xml for better readability.
/// </summary>
/// <param name="XmlString">The Xml to format.</param>
/// <returns>A string formatted as Xml with indenting.</returns>
public static string FormatXml(string xmlString)
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(xmlString);

StringWriter sW = new StringWriter();

XmlTextWriter XmlW = new XmlTextWriter(sW);
XmlW.Formatting = Formatting.Indented;

XmlDoc.DocumentElement.WriteContentTo(XmlW);

XmlW.Close();
return sW.ToString();
}

--
venlig hilsen / with regards
anders borum
--


Nov 12 '05 #4

Oleg Tkachenko
P: n/a
Oleg Tkachenko
Anders Borum wrote:
[color=blue]
> Just create an XmlTextWriter with indenting as formatting. Then run your
> unformatted string through the writer and voila, a perfectly formatting Xml
> document as string. This is also called pretty-printing!
>
> /// <summary>
> /// Formats the string Xml for better readability.
> /// </summary>
> /// <param name="XmlString">The Xml to format.</param>
> /// <returns>A string formatted as Xml with indenting.</returns>
> public static string FormatXml(string xmlString)
> {
> XmlDocument XmlDoc = new XmlDocument();
> XmlDoc.LoadXml(xmlString);
>
> StringWriter sW = new StringWriter();
>
> XmlTextWriter XmlW = new XmlTextWriter(sW);
> XmlW.Formatting = Formatting.Indented;
>
> XmlDoc.DocumentElement.WriteContentTo(XmlW);
>
> XmlW.Close();
> return sW.ToString();
> }[/color]

Sorry for nitpicking again, Anders ;)

There is much more effective way of indenting XML, which doesn't require
to load the whole document into memory - XmlReader/XmlWriter pipeline.
It should be much faster and memory friendly:

XmlTextReader r = new XmlTextReader(new StringReader(xmlString));
r.MoveToContent();
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTextWriter(sw);
w.Formatting = Formatting.Indented;
w.WriteNode(r,false);
w.Close();
return sw.ToString();

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #5

Anders Borum
P: n/a
Anders Borum
Hello Oleg!

Thanks for the heads up. I was simply illustrating how to format an XML
string, but you managed to optimize the example. Actually I'm using the
method in a framework and at some time, I'd have to go over it again.
There's really no need to load the entire string to memory, when all we need
is a simple read-to-end.

Thanks again. You nitpickin' .. <insert lotsa stuff here> ;-)

--
venlig hilsen / with regards
anders borum
--


Nov 12 '05 #6

Post your reply

Help answer this question



Didn't find the answer to your .NET Framework question?

You can also browse similar questions: .NET Framework