Hi badgriffin ,
Change your code to :
XmlWriterSettings outputSettings = new XmlWriterSettings();
outputSettings.Encoding = Encoding.UTF8;
outputSettings.NewLineOnAttributes = true;
outputSettings.Indent = true;
outputSettings.IndentChars = ("\t");
string defaultNamespace = "http://foo/default/";
string extraNamespace = "http://foo/extra/";
XmlWriter output = XmlWriter.Create(Server.MapPath("foo.xml"), outputSettings);
output.WriteStartElement("Foo", defaultNamespace);
output.WriteAttributeString("xmlns", "extra", null, extraNamespace);
// As expected, this attribute is emitted without a namespace prefix and is, by definition,
// in the namespace defaultNamespace.
output.WriteAttributeString("localName-value-only", "value");
// As expected, the XmlWriter already knows that extraNamespace has been mapped to "extra" so this
// attribute is emitted with a prefix of "extra:".
output.WriteAttributeString("localName-nsExtra-value", extraNamespace, "value");
// At this point, the XmlWriter knows that the default namespace is defaultNamespace and that
// the extraNamespace uses the prefix "extra:". We've already proven that when we emit an attribute
// using the extraNamespace that the XmlWriter uses the correct prefix of "extra:". So, why do each
// of the following lines of code cause the XmlWriter to unexpected ignore the default namespace and
// create a new prefix p2 with the same EXACT namespace as the default namespace and then use the
// "p2:" prefix on each of these attributes. They should be emitted without any namespace prefix
// because they are all in the default namespace. If you comment out two of these and leave only
// one of them you still get the same "p2:" prefixed results. I was hoping that one of the variations
// would get the intended results but they all seem to produce the same results.
output.WriteAttributeString("localName-nsDefault-value", null, "value");
output.WriteAttributeString("", "prefixEmpty-localName-nsDefault-value", null, "value");
output.WriteAttributeString(null, "prefixNull-localName-nsDefault-value", null, "value");
// Again, the XmlWriter uses the "p2:" prefix on this element even though it is clearly in the default
// namespace and doesn't need a prefix.
output.WriteStartElement("Bar", defaultNamespace);
output.WriteEndElement(); // Bar
output.WriteEndElement(); // Foo
output.Flush();
output.Close();