简体   繁体   中英

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 

....

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM