I did a bit of testing and determined you need to use one of the overloads of XmlWriter.WriteStartElement, specifically;
XmlWriter.WriteStartElement Method (String, String, String) (System.Xml)[
^]
The below snippet does work & will give you the output that you have requested in your comment;
Dim sett as XmlWriter.Settings = New XMLWriterSettings();
sett.Indent = True
using writer as XmlWriter = XmlWriter.Create("Filename and Path", sett)
writer.WriteStartElement("cfdi", "Comprobante", "http://www.sat.gob.mx/cfd/3")
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-Instance")
writer.WriteAttributeString("xmlns", "schemaLocation", null, "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cdv33.xsd")
writer.WriteEndElement()
New version - open a brand new C# Windows Form project, add a single button to the form. The below code can be entered into the button_click event;
private void button1_Click(object sender, EventArgs e)
{
XmlWriter writer = null;
try
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create("FileName.xml", settings);
writer.WriteComment("sample XML fragment");
writer.WriteStartElement("cfdi", "Comprobante", "http://www.sat.gob.mx/cfd/3");
writer.WriteAttributeString("xmlns", "xsi", null, @"http://www.w3.org/2001/XMLSchema-Instance");
writer.WriteAttributeString("xmlns", "schemaLoc", null, @"http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cdv33.xsd");
writer.WriteStartElement("cfdi", "ElementName", "http://www.sat.gob.mx/cfd/3");
writer.WriteString("This is my element string");
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
finally
{
if (writer != null)
writer.Close();
}
}
This will output the following XML;
<?xml version="1.0" encoding="utf-8"?>
<!--sample XML fragment-->
<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance" xmlns:schemaLoc="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cdv33.xsd" xmlns:cfdi="http://www.sat.gob.mx/cfd/3">
<cfdi:elementname>This is my element string
Kind Regards