This shoud work
using System.Xml;
namespace Dummy
{
public class Sample
{
public void GenerateXmlDocument()
{
XmlDocument xdoc = new XmlDocument();
XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
xdoc.AppendChild(dec);
XmlNode rootNode = xdoc.CreateNode(XmlNodeType.Element, "ROOTElement", "");
XmlAttribute att1 = xdoc.CreateAttribute("xmlns:xsi");
att1.Value = "http://www.w3.org/2001/XMLSchema-instance";
rootNode.Attributes.Append(att1);
XmlAttribute att2 = xdoc.CreateAttribute("xsi:noNamespaceSchemaLocation");
att2.Value = "yourschema.xsd";
rootNode.Attributes.Append(att2);
xdoc.AppendChild(rootNode);
xdoc.Save(@"c:\sample.xml");
}
}
}
- Modded code to show adding a child element
using System.Xml;
namespace Dummy
{
public class Sample
{
public void GenerateXmlDocument()
{
XmlDocument xdoc = new XmlDocument();
XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
xdoc.AppendChild(dec);
XmlNode rootNode = xdoc.CreateNode(XmlNodeType.Element, "ROOTElement", "");
XmlAttribute att1 = xdoc.CreateAttribute("xmlns:xsi");
att1.Value = "http://www.w3.org/2001/XMLSchema-instance";
rootNode.Attributes.Append(att1);
XmlAttribute att2 = xdoc.CreateAttribute("xsi:noNamespaceSchemaLocation");
att2.Value = "yourschema.xsd";
rootNode.Attributes.Append(att2);
xdoc.AppendChild(rootNode);
XmlElement XElemRoot = xdoc.CreateElement("ACES");
rootNode.AppendChild(XElemRoot);
XmlElement Xsource = xdoc.CreateElement("RETURN");
Xsource.SetAttribute("ReturnType", "DLR");
Xsource.SetAttribute("ToolVer", "1.0");
XElemRoot.AppendChild(Xsource);
xdoc.Save(@"c:\sample.xml");
}
}
}
This gives
="1.0"="utf-8"
<ROOTElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="yourschema.xsd">
<ACES>
<RETURN ReturnType="DLR" ToolVer="1.0" />
</ACES>
</ROOTElement>