473,500 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transform XML

Hey All,

I have a string that contains XML content and I want to transform it
in new XML content string.

What is the best way of doing this?

private string DoXSLTransform(string aXSLFileName, string
sOrigXMLContent)
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(aXSLFileName);

//copy my string into the XmlTextReader
XmlTextReader readerOrigContent =
new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(sOrigX MLContent)));

//create my stream for the result
Stream resultStream;
//do the transformation
xslt.Transform(readerOrigContent, null, resultStream);

//copy my stream into the StreamReader so I can get the string
StreamReader readerResult = new StreamReader(resultStream);
//get the resuling xml file as a string
return readerResult.ReadToEnd();
}

TIA!

~GINA_M~

Jun 11 '07 #1
6 2678
I think i may have found it in another post:

private string DoXSLTransform(string aXSLFileName, string
sOrigXMLContent)
{
//copy my string into the XmlTextReader
XmlTextReader readerOrigContent =
new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(sOrigX MLContent)));

XslCompiledTransform myXslTransform = new XslCompiledTransform();

// Prepare an output stream based on a System.Text.StringBuilder():
StringBuilder sbResult = new StringBuilder();
StringWriter sResult = new StringWriter(sbResult);
XmlTextWriter sXML = new XmlTextWriter(sResult);

// Load the XSL, your code:
myXslTransform.Load(aXSLFileName);
// Perform transform, your code modified to use new stream:
myXslTransform.Transform(readerOrigContent, null, sXML);
// Make sure the stream's written everything to the underlying
buffer:
sXML.Flush();
// Retrieve string:
return sbResult.ToString();
}

I will let you know the outcome.

~GINA_M~

Jun 11 '07 #2
ko
On Jun 11, 4:49 pm, Gina_Marano <ginals...@gmail.comwrote:
I think i may have found it in another post:

private string DoXSLTransform(string aXSLFileName, string
sOrigXMLContent)
{
//copy my string into the XmlTextReader
XmlTextReader readerOrigContent =
new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(sOrigX MLContent)));

XslCompiledTransform myXslTransform = new XslCompiledTransform();

// Prepare an output stream based on a System.Text.StringBuilder():
StringBuilder sbResult = new StringBuilder();
StringWriter sResult = new StringWriter(sbResult);
XmlTextWriter sXML = new XmlTextWriter(sResult);

// Load the XSL, your code:
myXslTransform.Load(aXSLFileName);
// Perform transform, your code modified to use new stream:
myXslTransform.Transform(readerOrigContent, null, sXML);
// Make sure the stream's written everything to the underlying
buffer:
sXML.Flush();
// Retrieve string:
return sbResult.ToString();

}

I will let you know the outcome.

~GINA_M~
Hi Gina,

If that didn't work try this:

private static string xsl_transform(string xsl_file, string
xml_content) {
// XmlReader.Create(TextReader) - StringReader 'is a' TextReader
using (XmlReader xr = XmlReader.Create(new
StringReader(xml_content))) {
XslCompiledTransform xc = new XslCompiledTransform();
xc.Load(xsl_file);
StringBuilder sb = new StringBuilder();
// XmlTextWriter(TextWriter) - StringWriter 'is a' TextWriter
using (XmlTextWriter xt = new XmlTextWriter(new
StringWriter(sb))) {
xc.Transform(xr, xt);
}
return sb.ToString();
}
}

HTH - keith

Jun 12 '07 #3
Gina_Marano <gi*******@gmail.comwrote:

<snip>
new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(sOrigX MLContent)));
As I've already said, this is a horrible way of loading a string into
an XmlTextReader. Use StringReader.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 12 '07 #4
Can you type up a little example?
As I've already said, this is a horrible way of loading a string into
an XmlTextReader. Use StringReader.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 15 '07 #5
Gina_Marano <gi*******@gmail.comwrote:
As I've already said, this is a horrible way of loading a string into
an XmlTextReader. Use StringReader.
Can you type up a little example?
string x = "<?xml version=\"1.0\"><foo/>";

using (StringReader reader = new StringReader(x))
{
// do what you want with "reader", e.g. passing it to
// the constructor of XmlTextReader
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 16 '07 #6
Thanks Jon!

~Gina_M~

Jun 25 '07 #7

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

Similar topics

5
4652
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
0
2995
by: Xiaolei Li | last post by:
first off, i'm a total newbie at this stuff so excuse any wrong usage of terminology or whatever else. i have a XSL to transform a Document such that all "text" nodes will have a "SPAN" inserted...
2
1024
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...
9
808
by: Patrick Guio | last post by:
Dear all, I am trying to use the std::transform algorithm to to the following vector< vector<char> >::iterator ik = keys.begin(); // key list iterator vector< vector<char> >::iterator is = ik;...
6
2467
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...
3
2450
by: Jason S | last post by:
Hello Group, I am just about tearing my hair out with this one and thought someone may have some insight. I have a transform that wasn't working so I grabbed the nearest debugger (xselerator)...
4
4799
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt...
1
1831
by: Mike Hofer | last post by:
I've got two statements in my code that are both generating weird, weird, weird messages: The first one was, Dim document As System.Xml.XmlDocument Dim navigator As...
4
8500
by: Dean Card | last post by:
Okay, so here is the situation. I have need to do some on-the-fly image creation. I have everything working great except for the last part of it, applying a perspective type transform to the...
6
4935
by: Vijai Kalyan | last post by:
Hello, I am trying to use std::transform to take in a collection of strings, transform them and insert the result into an output container. So, I wrote something like this: std::wstring...
0
7153
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
7027
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7215
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,...
1
6919
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
7405
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...
0
5510
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3113
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1442
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.