0

I am trying to add a namespace tag to an XML document using system.XML in C# Here is my code so far:

XmlDocument doc = new XmlDocument();
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);

XmlElement invHeaderTag = doc.CreateElement(string.Empty, "InvoiceHeader", string.Empty);
doc.AppendChild(invHeaderTag);

The image below show the XML I am trying to create.

enter image description here

How do I create the namespace tag?

Thomas Adrian
  • 3,543
  • 6
  • 32
  • 62
  • Did you try `doc.DocumentElement.SetAttribute("xmlns:ns0", "othernamespace_Invoices");`? – Selim Yildiz Mar 16 '20 at 07:59
  • thanks for your input, can you please expalin where to put that, I tried this: doc.DocumentElement.SetAttribute("xmlns:ns0", "_Invoices"); without success – Thomas Adrian Mar 16 '20 at 08:08
  • Try to use XDocument and XElement it is much easier to use and will save a lot of youre time. – WhoKnows Mar 16 '20 at 08:16
  • I understand, so there is no answer to my question. you are refering to something else, and Alexei refer to a post that has no accepted answer – Thomas Adrian Mar 16 '20 at 08:50

0 Answers0