+ public string GetNormalizedSchema (string source)
+ {
+/*
+ // Due to the implementation difference, we must have
+ // one more step to reorder attributes. Here, read
+ // schema document into XmlSchema once, and compare
+ // output string with those emission from Write().
+ XmlSchema xs = XmlSchema.Read (new XmlTextReader (
+ new StringReader (source)), null);
+ StringWriter writer = new StringWriter ();
+ xs.Write (writer);
+ return writer.ToString ();
+*/
+ XmlDocument doc = new XmlDocument ();
+ doc.LoadXml (source);
+ SortAttributes (doc.DocumentElement);
+ StringWriter writer = new StringWriter ();
+ doc.Save (writer);
+ return writer.ToString ();
+ }
+
+ private void SortAttributes (XmlElement el)
+ {
+ SortAttributesAttributes (el);
+ ArrayList al = new ArrayList ();
+ foreach (XmlNode n in el.ChildNodes) {
+ if (n.NodeType == XmlNodeType.Element)
+ SortAttributes (n as XmlElement);
+ if (n.NodeType == XmlNodeType.Comment)
+ al.Add (n);
+ }
+ foreach (XmlNode n in al)
+ el.RemoveChild (n);
+ }
+
+ private void SortAttributesAttributes (XmlElement el)
+ {
+ ArrayList al = new ArrayList ();
+ foreach (XmlAttribute a in el.Attributes)
+ al.Add (a.Name);
+ al.Sort ();
+ string [] names = (string []) al.ToArray (typeof (string));
+ al.Clear ();
+ foreach (string name in names)
+ al.Add (el.RemoveAttributeNode (
+ el.GetAttributeNode (name)));
+ foreach (XmlAttribute a in al)
+ // Exclude xmlns="" here.
+ if (a.Name != "xmlns" || a.Value != String.Empty)
+ el.SetAttributeNode (a);
+ }
+