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

前端 未结 7 1268
生来不讨喜
生来不讨喜 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:26

    If possible, create a serialization class then do:

    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("", "");
    XmlSerializer serializer = new XmlSerializer(yourType);
    serializer.Serialize(xmlTextWriter, someObject, ns);
    

    It's safer, and you can control the namespaces with attributes if you really need more control.

提交回复
热议问题