I'm struggling a bit trying to find the appropriate way of writing exactly this XML with XmlWriter and underlying string builder:
<x:node xmlns="uri:default"
xmlns:x="uri:special-x"
xmlns:y="uri:special-y"
y:name="MyNode"
SomeOtherAttr="ok">
</x:node>
The best I have so far:
static string GetXml()
{
var r = new StringBuilder();
var w = XmlWriter.Create(r, new XmlWriterSettings { OmitXmlDeclaration = true });
w.WriteStartElement("x", "node", "uri:special-x");
w.Flush();
r.Append("\n" + new string(' ', 7));
w.WriteAttributeString("xmlns", "x", null, "uri:special-x");
w.Flush();
r.Append("\n" + new string(' ', 7));
w.WriteAttributeString("xmlns", "y", null, "uri:special-y");
w.Flush();
r.Append("\n" + new string(' ', 7));
w.WriteAttributeString("name", "uri:special-y", "vd");
w.Flush();
r.Append("\n" + new string(' ', 7));
w.WriteAttributeString("SomeOtherAttr", "ok");
w.Flush();
w.WriteEndElement();
w.Flush();
return r.ToString();
}
which creates
<x:node
xmlns:x="uri:special-x"
xmlns:y="uri:special-y"
y:name="vd"
SomeOtherAttr="ok" />
but I cannot find a way to write default xmlns right after the node. Any try leads to error or different formatting.
Any ideas? Thanks!
Update: maybe I can write it directly to the StringBuilder but I look for more... hm.. correct approach.