This seems to do what you want:
using System;
using System.Xml;
class Test
{
public static void Main(string[] args)
{
using (var writer = XmlWriter.Create(Console.Out))
{
writer.WriteStartElement("ns1", "foo", "http://someurl/schemas");
writer.WriteAttributeString("xmlns", "ns1", null, "http://someurl/schemas");
writer.WriteEndElement();
}
}
}
Output (leaving out the XML declaration):
<ns1:foo xmlns:ns1="http://someurl/schemas" />
Looking at the documentation for that overload of WriteStartElement
it should be clear why that works, as these are the parameters in order:
- prefix
Type: System.String
The namespace prefix of the element.- localName
Type: System.String
The local name of the element.- ns
Type: System.String
The namespace URI to associate with the element.