1

I´m trying to transform the whole input xml to a string in the output xml. And i´m almost there. I have manage to get all the content into the string element, but i´m missing the xml declaration. I need this because of the charset information.

Anyone have an idea on how to manage this?

I currently use this c# method to do the work:

public static string ConvertNodeToXmlString(XPathNodeIterator node)
{
        node.MoveNext();
        return node.Current.OuterXml;
}

and it´s called from xslt:

<xsl:variable name="result" xmlns:myScriptPrefix="http://HelperClass" select="myScriptPrefix:ConvertNodeToXmlString(.)" />

All help is much appreciated!

  • 1
    Might be worth mentioning that you should avoid inline c# at all cost due to memory leaks sometimes happening. Try putting them in an extension object to avoid that. – zurebe-pieter Nov 14 '13 at 23:54

1 Answers1

0

Well which encoding do you want? You could use http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.writesubtree%28v=vs.110%29.aspx

node.MoveNext();

using (StringWriter sw = new StringWriter())
{
  using (XmlWriter xw = XmlWriter.Create(sw))
  {
    node.Current.WriteSubtree(xw);
  }
  return sw.ToString();
}

but as .NET Strings are UTF-16 encoded you might get <?xml version="1.0" encoding="UTF-16"?>.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Hi! I want to pass and preserve the encoding currently in the declaration. I think it is ISO-8859-1. – user2783955 Nov 14 '13 at 10:17
  • @user2783955, well the XSLT data model does not store the encoding of the input source so the code using `WriteSubtree` will not know the original encoding. – Martin Honnen Nov 14 '13 at 10:21
  • Ok, i´m on thin ice here since i have not done that much inline xslt code. But will xslt change the encoding before it´s sent to the c# assembly? And is it possible to change the encoding to ISO-8859-1 from the c# method? – user2783955 Nov 14 '13 at 10:36
  • XSLT/XPath operates on `XPathNavigator`s and `XPathNodeIterator`s and the C# code as well. Those don't store any encoding (other than using the .NET data type `System.String` which is a sequence of UTF-16 encoded characters). Before spending too much energy on outputting a particular encoding, can we take step back so that you tell us what you want to achieve? Why do you want to pass a complete XML document through XSLT only (?) to pass it on to an extension that serializes it to a string? – Martin Honnen Nov 14 '13 at 11:07
  • Thanx for the explaination! I´m actually doing this in a biztalk map, but overrides with inline xslt. I´m going to enque a xml message to an oracle aq, and the way of doing this is mapping the whole xml to one input parameter of the auto-generated schema(this is generated based on a procedure in the database). And it´s important that the charset is correct so it´s readable by the dequeuing system. – user2783955 Nov 14 '13 at 13:51
  • I am afraid I am not familiar with things like biztalk or oracle aq. Maybe someone else can help further. See http://stackoverflow.com/questions/9459184/why-is-the-xmlwriter-always-outputing-utf-16-encoding for ways how to create a StringWriterWithEncoding in .NET that does not assume UTF-16. – Martin Honnen Nov 14 '13 at 14:31