public void WriteXmlExplicit(Stream w, Encoding encoding) { // to keep control of opened tags Stack <string> levels = new Stack <string>(); // list of column to create xml structure List <string> scColumns = GetColumnsAlias(); int scCount = scColumns.Count; List <XmlExplicitColumnInfo> xeColumns = new List <XmlExplicitColumnInfo>(); for (int x = 2; x < scCount; x++) { xeColumns.Add(new XmlExplicitColumnInfo(scColumns[x])); } XmlTextWriter writer = new XmlTextWriter(w, encoding); IDataReader reader = ExecuteReader(); int xeCount = xeColumns.Count; string text; while (reader.Read()) { string tag = reader.GetValue(0).ToString(); string parent = ""; if (!reader.IsDBNull(1)) { parent = reader.GetValue(1).ToString(); } bool first = true; for (int x = 0; x < xeCount; x++) { XmlExplicitColumnInfo col = xeColumns[x]; if (col.TagNumber == tag) { if (first) { int count = levels.Count; if (levels.Count == 0) { levels.Push(String.Format("{0}-{1}", tag, parent)); } else { while (levels.Count > 0) { string[] last = levels.Peek().Split('-'); // if is brother if ((tag == last[0] && parent == last[1])) { writer.WriteEndElement(); break; } // if is son else if (parent == last[0]) { levels.Push(String.Format("{0}-{1}", tag, parent)); break; } else { writer.WriteEndElement(); levels.Pop(); } } } writer.WriteStartElement(col.ElementName); first = false; } if (reader.IsDBNull(x + 2)) { text = String.Empty; } else { if (reader.GetDataTypeName(x + 2) == "datetime") { text = reader.GetDateTime(x + 2).ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ"); } else { text = reader.GetValue(x + 2).ToString(); } } if (col.Directive == "element") { writer.WriteStartElement(col.AttributeName); writer.WriteString(text); writer.WriteEndElement(); } else if (col.Directive == "cdata") { writer.WriteCData(text); } else if (col.Directive == "xml") { writer.WriteRaw(text); } else if (col.Directive == "hide") { } else { writer.WriteAttributeString(col.AttributeName, text); } } } } reader.Close(); for (var x = 0; x < levels.Count; x++) { writer.WriteEndElement(); } writer.Flush(); }
public void WriteXmlAuto(Stream w, Encoding encoding) { // to keep control of opened tags Hashtable htTags = new Hashtable(); Hashtable htTracker = new Hashtable(); // list of column to create xml structure List <string> scColumns = GetColumnsAlias(); int scCount = scColumns.Count; List <XmlExplicitColumnInfo> xeColumns = new List <XmlExplicitColumnInfo>(); int tagsCount = 0; string tag = ""; for (int x = 0; x < scCount; x++) { XmlExplicitColumnInfo xeci = new XmlExplicitColumnInfo(scColumns[x]); if (xeci.TagNumber != tag) { tag = xeci.TagNumber; tagsCount++; htTags[tag] = x; } xeColumns.Add(xeci); } XmlTextWriter writer = new XmlTextWriter(w, encoding); IDataReader reader = ExecuteReader(); int xeCount = xeColumns.Count; int openLevels = 0; string text; while (reader.Read()) { for (int t = 1; t <= htTags.Count; t++) { int x = (int)htTags[t.ToString()]; XmlExplicitColumnInfo col = xeColumns[x]; if (t == tagsCount) { writer.WriteStartElement(col.ElementName); while (x < xeCount) { col = xeColumns[x]; if (reader.IsDBNull(x)) { text = String.Empty; } else { if (reader.GetDataTypeName(x) == "datetime") { text = reader.GetDateTime(x).ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ"); } else { text = reader.GetValue(x).ToString(); } } if (col.Directive == "element") { writer.WriteStartElement(col.AttributeName); writer.WriteString(text); writer.WriteEndElement(); } if (col.Directive == "cdata") { writer.WriteCData(text); } if (col.Directive == "xml") { writer.WriteRaw(text); } else if (col.Directive == "") { writer.WriteAttributeString(col.AttributeName, text); } x++; } writer.WriteEndElement(); } else { if (reader.IsDBNull(x)) { text = String.Empty; } else { if (reader.GetDataTypeName(x) == "datetime") { text = reader.GetDateTime(x).ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ"); } else { text = reader.GetValue(x).ToString(); } } if (htTracker[col.TagNumber] == null || htTracker[col.TagNumber].ToString() != text) { if (htTracker[col.TagNumber] != null) { writer.WriteEndElement(); openLevels--; } htTracker[col.TagNumber] = text; openLevels++; writer.WriteStartElement(col.ElementName); while (x < xeCount && col.TagNumber == t.ToString()) { if (reader.IsDBNull(x)) { text = String.Empty; } else { if (reader.GetDataTypeName(x) == "datetime") { text = reader.GetDateTime(x).ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ"); } else { text = reader.GetValue(x).ToString(); } } if (col.Directive == "element") { writer.WriteStartElement(col.AttributeName); writer.WriteString(text); writer.WriteEndElement(); } if (col.Directive == "cdata") { writer.WriteCData(text); } if (col.Directive == "xml") { writer.WriteRaw(text); } else if (col.Directive == "") { writer.WriteAttributeString(col.AttributeName, text); } x++; if (x < xeCount) { col = xeColumns[x]; } } } } } } reader.Close(); for (int x = 0; x < openLevels; x++) { writer.WriteEndElement(); } writer.Flush(); }