[quoted text, click to view] "Fredrik Melin" <mel@n.o.spam.dacsa.net.remove.as.needed> wrote in message
news:e7tEbL4vEHA.3976@TK2MSFTNGP09.phx.gbl...
> Is there anyway to change so it converts to <OrderIDInfo attributes... />
There is no way to change the XSLT stylesheet to produce XML with compact
elements, because the XSLT specification purposefully leaves this an
implementation
detail. As far as XML is concerned, <OrderIDInfo></OrderIDInfo> and the
more
compact <OrderIDInfo /> are identical serializations of an XML element with
the
local name OrderIDInfo and no child nodes.
By default, the XslTransform implementation emits full end tags (probably
because
it's just simpler that way, there is less state to maintain in the processor
when it does
not need to remember the current element is empty).
Interestingly, the reason why this is a problem for you is also precisely
the answer.
XslTransform produces a resulting node tree, you can insert an XmlTextWriter
implementation that dictates how that node tree should appear in the
serialization
of the XML.
Wrap an XmlTextWriter around the output from the XslTransform's Transform( )
method and intercepting calls that XslTransform makes to the
WriteFullEndElement( )
method into calls that your custom XmlTextWriter makes to
WriteEndElement( ).
It's the WriteEndElement( ) that produces the more compact empty element (it
also maintains state about the existence of any child nodes, and so produces
a
full end tag where one is necessary).
- - - XmlTextWriterEE.cs
using System;
using System.IO;
using System.Xml;
/// <summary>
/// Wrapper that forces more compact empty element end
/// tags to be written whenever possible.
/// </summary>
public class XmlTextWriterEE : XmlTextWriter
{
public XmlTextWriterEE( TextWriter sink) : base( sink) {;}
public override void WriteFullEndElement() {
base.WriteEndElement();
}
}
- - -
This XmlTextWriter would work something like this in your application,
xslDoc.Transform( xmlDoc.CreateNavigator( ),
null, new XmlTextWriterEE( Console.Out) );
Derek Harmon