views:

88

answers:

2

I create a MSXML6 DOM document and during serialization I want to control how empty elements are serialized:

  1. <tag></tag>

  2. <tag/>

This answer describes a solution for C#, but I'm looking for something possible with the ActiveX interface of MSXML. (For VB6 or some scripting language)

A: 

Shouldn't be hard to create a COM wrapper that exposes to your VB6 app the necessary C# magic capability.

using Interop=System.Runtime.InteropServices;

namespace MyNamespace
{
    [Interop.GuidAttribute("...guid here...")]
    [Interop.ComVisible(true)]
    [Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)]
    public partial class MyXmlWrapper 
    {
        // expose methods in here that do the XML serialization the 
        // way you want to.  
    }
}

If you haven't done this before, it might seem exotic to define COM objects in C#, but it's not that difficult.

Cheeso
+1  A: 

This is incredibly messy, but I've discovered that if you use the createElement method on a MSXML document you get (for some reason) an xml element that serailizes to the <tag /> format, and so you can force elements to serailize like this by replacing them with elements you create with the same name:

<!-- Contents of c:\xml.xml -->
<xml>
    <element></element>
</xml>

In Javascript (but easy to convert to VbScript hopefully)

objXML = new ActiveXObject("MSXML2.DOMDocument.4.0");
objXML.load("c:\\xml.xml");

var xmlElement = objXML.childNodes[1];

var newElement = objXML.createElement(xmlElement.childNodes[0].tagName);
xmlElement.replaceChild(newElement, xmlElement.childNodes[0]);

Conversely, you can force unexpanded <tag /> elements to expand out by setting the text property to be "":

newElement.text = "";

Hope this helps - I know this is really really horrible, but the chances are the fact that you need to be able to do this in the first place is horrible enough already so this extra horribleness wont make much difference! :-p

Kragen