简体   繁体   中英

XmlWriter Not Allowing Me to Ignore LocalName When Writing Namespace

How would I create this string using the XMlWriter class? I keep getting an error.

Here is the XML string I am trying to create:

<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200">

Here is the code I am trying to generate it with:

using (XmlWriter writer = XmlWriter.Create(@"C:\Temp\my.xml")) 
{
    writer.WriteStartElement("svg");
    writer.WriteAttributeString("`xmlns`", null, "http://www.w3.org/2000/svg");
    writer.WriteEndElement();
} 

Normally you don't need to write your own attribute prefix mapping for element's namespace. In this case it looks like properly specifying namespace for "svg" element is what you are after:

using (XmlWriter writer = XmlWriter.Create(@"C:\Temp\my.xml"))
{
    writer.WriteStartElement(null, "svg","http://www.w3.org/2000/svg" );
    writer.WriteEndElement();
}

Note that code in the post is writing "svg" with empty/default namespace, which is different from having empty namespace prefix .

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