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

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

    If the element in your sample XML didn't have the xmlns default namespace declaration on it, then it would be in the whatever:name-space-1.0 namespace rather than being in no namespace. If that's what you want, you need to create the element in that namespace:

    xml.CreateElement("loner", "whatever:name-space-1.0")
    

    If you want the element to be in no namespace, then the XML that's been produced is exactly what you need, and you shouldn't worry about the xmlns attribute that's been added automatically for you.

提交回复
热议问题