473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Transform XML string using XSL file (v1.1)

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
5 6146
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" <sp**@aconnell.com> wrote in message
news:24**************************@posting.google.c om...
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 #2
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
Hello!
I get a string of xml data with no formatting!
the elements are concatenated


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
Anders Borum wrote:
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();
}


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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Lehmann | last post by:
I have an interesting problem. I am performing an XSL transform using the System.Xml.Xsl.Transform class. I have a database that contains the XSL style sheet string. And it seems to work pretty...
6
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
6
by: tcdevelopment | last post by:
I have a XSLT file that gives expected results when I transform using MSXML V4.0 in a simple vbs file. However when using XslTransform in dotnet, I do not get the same results. The part of the...
2
by: K riley | last post by:
Hi. I am using the transform method to create an HTML file using an existing XML Document and an XLST. I need to use XPath to extract a node from my XML Document and my XLST document will format...
4
by: Isambella via DotNetMonster.com | last post by:
Hi, I have in a string some XML and I want that xml to be transformed using XSLT file (I found the way how to transform a xml file using XSLT but I didn’t find a way how to transform using...
9
by: Doug Stiers | last post by:
I have this vb.net (framework 1.1) code: Dim x As Xml.Xsl.XslTransform = New Xml.Xsl.XslTransform Dim xr As XmlResolver MessageBox.Show("before load") x.Load(<xsl file name>) -- THIS IS WHERE...
0
by: Terry Brown | last post by:
I have an xml file: <?xml version="1.0" encoding="utf-8" ?> <G2Registers xmlns="http://tempuri.org/registers.xsd"> <register> <name>Version Register</name> <address>"00000000"</address>...
1
by: Steve | last post by:
Using VB.NET 2.0 I have a simple routine that attempts transforms an XmlDocument with an XSLT stylesheet into HTML. Under the old 1.1 framework with XslTransform, everything worked fine. Now...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.