How to prevent blank xmlns attributes in output from .NET's XmlDocument?

前端 未结 7 1311
生来不讨喜
生来不讨喜 2020-11-27 03:56

When generating XML from XmlDocument in .NET, a blank xmlns attribute appears the first time an element without an associated namespace is inserted; ho

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 04:33

    Since root is in an unprefixed namespace, any child of root that wants to be un-namespaced has to be output like your example. The solution would be to prefix the root element like so:

    
       
    
    

    code:

    XmlDocument doc = new XmlDocument();
    XmlElement root = doc.CreateElement( "w", "root", "whatever:name-space-1.0" );
    doc.AppendChild( root );
    root.AppendChild( doc.CreateElement( "loner" ) );
    Console.WriteLine(doc.OuterXml);
    

提交回复
热议问题