How can I avoid redundant namespace declarations writing inner xml to XmlWriter?

When I am writing xml to an XmlWriter object, the below code works fine. However, if there is a namespace declared higher up and the xml I'm writing repeats it, it repeats it in the XML it writes. Legal but messy (we have a lot of very long namespaces).

Code we presently have:

public void addInnerXml(String xml) throws TinyException
{

    try {
        StringReader strReader = new StringReader(xml);
        XmlReader reader = XmlReader.Create(strReader);
        reader.MoveToContent();

        parent.adjustStack(this);
        parent.getDoc().WriteNode(reader, false);
        reader.Dispose();
        strReader.Dispose();

        // needed for declaration
        if (false)
            throw new cli.System.Exception();
    }
    catch (cli.System.Exception ex)
    {
        throw new TinyException(ex);
    }
}

ps - Looks a little weird because this is Java code compiled with IKVM that calls the .NET runtime.

728x90

0 Answers How can I avoid redundant namespace declarations writing inner xml to XmlWriter?