Namespaces in XML

Antonio asks:

I have to create this same structure using the DOM model. How do I create the namespace attributes?

<manifest identifier="MANIFEST1" xmlns:imsmd="**https://www.imsglobal.org/xsd/imsmd\_v1p2**" xsi:schemaLocation="https://www.imsglobal.org/xsd/imscp\_v1p1 imscp_v1p1p3.xsd https://www.imsglobal.org/xsd/imsmd\_v1p2 imsmd_v1p2p2.xsd" xmlns:xsi="**https://www.w3.org/2001/XMLSchema-instance**" xmlns="**https://www.imsglobal.org/xsd/imscp\_v1p1**" />

The problem in Antonio's case is the unused "imsmd" namespace prefix. This example shows how to create a namespace prefix as an attribute, how to create an attribute bound to a namespace, and how the namespace declaration is automatically generated based on a specified namespace prefix and namespace URI (notice how the xsi:schemaLocation attribute is created in this example):

XmlDocument doc = new XmlDocument();

XmlNode root = doc.AppendChild(doc.CreateElement("manifest","https://www.imsglobal.org/xsd/imscp_v1p1"));

XmlAttribute attrib = doc.CreateAttribute("identifier");

attrib.Value = "MANIFEST1";

root.Attributes.Append(attrib);

attrib = doc.CreateAttribute("xmlns:imsmd");

attrib.Value = "https://www.imsglobal.org/xsd/imsmd_v1p2";

root.Attributes.Append(attrib);

attrib = doc.CreateAttribute("xsi","schemaLocation","https://www.w3.org/2001/XMLSchema-instance");

attrib.Value = "https://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1p3.xsd https://www.imsglobal.org/xsd/imsmd\_v1p2 imsmd_v1p2p2.xsd";

root.Attributes.Append(attrib);

doc.WriteContentTo(new XmlTextWriter(Response.Output));