d3nver
d3nver

Reputation: 59

How to write a namespace in an Element with XmlWriter

I'm changing my code from vbs to C#, but i have a problem with XMLwriter, how to use XML write to get such output in XML.file : ??

I have code like this :

using (XmlWriter writer = XmlWriter.Create(Form1.systemDrive + "\\unattend.xml"))
{
    writer.WriteStartDocument();
    writer.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
    writer.WriteStartElement("unattend");
    writer.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:unattend"); // <<<<<<< Gives an error 

....

Upvotes: 2

Views: 2540

Answers (1)

TToni
TToni

Reputation: 9391

If you just want to put the unattend tag into the given namespace, don't use the WriteAttributeString at all and just use

writer.WriteStartElement("unattend","urn:schemas-microsoft-com:unattend");

If the namespace was assigned to a prefix, it will use the prefix, otherwise it will generate the necessary xmlns clause.

Also, the WriteStartDocument is redundant with the WriteProcessingInstruction. Both produce the same output, but you can only have one processing instruction, so you can use either one but not both.

Upvotes: 1

Related Questions