Controlling the order of XML namepaces

前端 未结 6 1163
你的背包
你的背包 2021-01-15 00:26

I\'m having a problem getting the \"xmlns\" to appear first in the root attribute list.

Im getting this:

  

        
6条回答
  •  广开言路
    2021-01-15 00:58

    I have a customer with this very problem. This was a real pain in the s, so I wrote a workaround to solve this.

    Please note this is not a beautiful solution, and this should be not encouraged, but works.

    public static class MyKludgeXmlClass
    {
        public static XmlDocument CreateXmlDocumentWithOrderedNamespaces()
        {
            var xml = "";
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(xml);
    
            return doc;
        }
    }
    

    With XmlDocument you can retrieve the root:

    var xmlDoc = MyKludgeXmlClass.CreateXmlDocumentWithOrderedNamespaces();
    XmlElement root = xmlDoc.DocumentElement;
    

    And append children nodes using your favorite method.

提交回复
热议问题