Andy Fish wrote:
I don't want to add a namespace declaration to every node - I want to have
one appear at the top of the document and be referenced automatically
whenever I add nodes.
That is not how the DOM works, the namespace is determined when a node
is being created and not change later depending on where you include,
insert, move, remove it or not.
If you want to create elements in a certain namespace then use the
overloads of CreateElement that allow you to specify namespaceURI and
prefix if needed. Do that for any element you create, later when you
save/serialize the serializer will take care of adding only those
xmlns="someURI" or xmlns:prefix="someURI" namespace declarations that
are necessary e.g.
const string exampleNamespaceURI = @"http://example.com/2006/ns1";
XmlDocument xmlDocument = new XmlDocument();
XmlElement gods = xmlDocument.CreateElement("", "gods",
exampleNamespaceURI);
XmlElement god = xmlDocument.CreateElement("", "god",
exampleNamespaceURI);
god.AppendChild(xmlDocument.CreateTextNode("Kibo") );
gods.AppendChild(god);
xmlDocument.AppendChild(gods);
xmlDocument.Save(Console.Out);
will be serialized/saved as
<gods xmlns="http://example.com/2006/ns1">
<god>Kibo</god>
</gods>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/