2 // System.Data/XmlTableWriter.cs
5 // Patrick Earl <mono@patearl.net>
7 // Copyright (c) 2006, Patrick Earl
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
34 internal class XmlTableWriter {
35 // This method is modelled after the DataSet's WriteXml functionality.
36 internal static void WriteTables(XmlWriter writer,
38 List<DataTable> tables,
39 List<DataRelation> relations,
43 if (mode == XmlWriteMode.DiffGram) {
44 foreach (DataTable table in tables)
46 DataSet.WriteDiffGramElement(writer);
49 bool shouldOutputContent = (mode != XmlWriteMode.DiffGram);
50 for (int n = 0; n < tables.Count && !shouldOutputContent; n++)
51 shouldOutputContent = tables[n].Rows.Count > 0;
53 if (shouldOutputContent) {
54 // We assume that tables[0] is the main table being written.
55 // We happen to know that the code above us does things that way.
56 DataSet.WriteStartElement(writer, mode, tables[0].Namespace, tables[0].Prefix, XmlHelper.Encode(dataSetName));
58 if (mode == XmlWriteMode.WriteSchema) {
59 DataTable [] _tables = new DataTable[tables.Count];
60 tables.CopyTo(_tables);
61 DataRelation[] _relations = new DataRelation[relations.Count];
62 relations.CopyTo(_relations);
63 new XmlSchemaWriter(writer,
71 WriteTableList (writer, mode, tables, DataRowVersion.Default);
73 writer.WriteEndElement();
76 if (mode == XmlWriteMode.DiffGram) {
77 List<DataTable> changedTables = new List<DataTable>();
78 foreach (DataTable table in tables) {
79 DataTable changed = table.GetChanges(DataRowState.Modified | DataRowState.Deleted);
80 if (changed != null && changed.Rows.Count > 0) {
81 changedTables.Add(changed);
84 if (changedTables.Count > 0) {
85 DataSet.WriteStartElement(writer, XmlWriteMode.DiffGram, XmlConstants.DiffgrNamespace, XmlConstants.DiffgrPrefix, "before");
86 WriteTableList (writer, mode, changedTables, DataRowVersion.Original);
87 writer.WriteEndElement();
90 writer.WriteEndElement(); // diffgr:diffgram
96 internal static void WriteTableList(XmlWriter writer, XmlWriteMode mode, List<DataTable> tables, DataRowVersion version)
98 foreach (DataTable table in tables)
99 DataSet.WriteTable(writer, table, mode, version);