How to add xmlnamespace to a xmldocument

This works for me:

XmlDocument.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlDocument.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");

If you want to create the entire document you’ve posted, you might not want to forget the XML declaration:

        XmlDeclaration xml_declaration;
        xml_declaration = XmlDocument.CreateXmlDeclaration("1.0", "ISO-8859-1", "yes");

        XmlElement document_element = XmlDocument.DocumentElement;
        XmlDocument.InsertBefore(xml_declaration, document_element);

In certain cases you might need it.

Leave a Comment