Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlDataTreeWriter.cs
Go to the documentation of this file.
5using System.Xml;
7
8namespace System.Data;
9
10internal sealed class XmlDataTreeWriter
11{
13
14 private readonly DataSet _ds;
15
16 private readonly DataTable _dt;
17
18 private readonly ArrayList _dTables = new ArrayList();
19
20 private readonly DataTable[] _topLevelTables;
21
22 private readonly bool _fFromTable;
23
24 private bool _isDiffgram;
25
27
28 private readonly bool _writeHierarchy;
29
31 {
32 _ds = ds;
33 _topLevelTables = ds.TopLevelTables();
34 foreach (DataTable table in ds.Tables)
35 {
36 _dTables.Add(table);
37 }
38 }
39
41 {
42 _dt = dt;
43 _fFromTable = true;
44 if (dt.DataSet == null)
45 {
47 _topLevelTables = new DataTable[1] { dt };
48 return;
49 }
50 _ds = dt.DataSet;
53 {
54 _writeHierarchy = true;
57 }
58 else
59 {
60 _topLevelTables = new DataTable[1] { dt };
61 }
62 }
63
65 {
67 for (int i = 0; i < _dTables.Count; i++)
68 {
70 if (dataTable.ParentRelations.Count == 0)
71 {
73 continue;
74 }
75 bool flag = false;
76 for (int j = 0; j < dataTable.ParentRelations.Count; j++)
77 {
78 if (dataTable.ParentRelations[j].Nested)
79 {
80 if (dataTable.ParentRelations[j].ParentTable == dataTable)
81 {
82 flag = false;
83 break;
84 }
85 flag = true;
86 }
87 }
88 if (!flag)
89 {
91 }
92 }
93 if (arrayList.Count == 0)
94 {
95 return Array.Empty<DataTable>();
96 }
97 DataTable[] array = new DataTable[arrayList.Count];
98 arrayList.CopyTo(array, 0);
99 return array;
100 }
101
103 {
104 foreach (DataRelation childRelation in dt.ChildRelations)
105 {
106 if (!_dTables.Contains(childRelation.ChildTable))
107 {
108 _dTables.Add(childRelation.ChildTable);
110 }
111 }
112 }
113
114 internal static bool RowHasErrors(DataRow row)
115 {
116 int count = row.Table.Columns.Count;
117 if (row.HasErrors && row.RowError.Length > 0)
118 {
119 return true;
120 }
121 for (int i = 0; i < count; i++)
122 {
124 string columnError = row.GetColumnError(column);
125 if (columnError != null && columnError.Length != 0)
126 {
127 return true;
128 }
129 }
130 return false;
131 }
132
133 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
135 {
137 _isDiffgram = true;
139 string prefix = ((_ds == null) ? ((_dt.Namespace.Length == 0) ? "" : _dt.Prefix) : ((_ds.Namespace.Length == 0) ? "" : _ds.Prefix));
140 if (_ds == null || _ds.DataSetName == null || _ds.DataSetName.Length == 0)
141 {
142 _xmlw.WriteStartElement(prefix, "DocumentElement", (_dt.Namespace == null) ? "" : _dt.Namespace);
143 }
144 else
145 {
147 }
148 for (int i = 0; i < _dTables.Count; i++)
149 {
151 foreach (DataRow row in dataTable.Rows)
152 {
153 if (row.RowState != DataRowState.Deleted)
154 {
155 int nestedParentCount = row.GetNestedParentCount();
156 if (nestedParentCount == 0)
157 {
159 XmlDataRowWriter(row, dataTable2.EncodedTableName);
160 }
161 else if (nestedParentCount > 1)
162 {
163 throw ExceptionBuilder.MultipleParentRows((dataTable.Namespace.Length == 0) ? dataTable.TableName : (dataTable.Namespace + dataTable.TableName));
164 }
165 }
166 }
167 }
169 _xmlw.Flush();
170 }
171
172 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
173 internal void Save(XmlWriter xw, bool writeSchema)
174 {
176 int num = _topLevelTables.Length;
177 bool flag = true;
178 string prefix = ((_ds == null) ? ((_dt.Namespace.Length == 0) ? "" : _dt.Prefix) : ((_ds.Namespace.Length == 0) ? "" : _ds.Prefix));
179 if (!writeSchema && _ds != null && _ds._fTopLevelTable && num == 1 && _ds.TopLevelTables()[0].Rows.Count == 1)
180 {
181 flag = false;
182 }
183 if (flag)
184 {
185 if (_ds == null)
186 {
187 _xmlw.WriteStartElement(prefix, "DocumentElement", _dt.Namespace);
188 }
189 else if (_ds.DataSetName == null || _ds.DataSetName.Length == 0)
190 {
191 _xmlw.WriteStartElement(prefix, "DocumentElement", _ds.Namespace);
192 }
193 else
194 {
196 }
197 for (int i = 0; i < _dTables.Count; i++)
198 {
199 if (((DataTable)_dTables[i])._xmlText != null)
200 {
201 _xmlw.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", "http://www.w3.org/2001/XMLSchema-instance");
202 break;
203 }
204 }
205 if (writeSchema)
206 {
207 if (!_fFromTable)
208 {
209 new XmlTreeGen(SchemaFormat.Public).Save(_ds, _xmlw);
210 }
211 else
212 {
214 }
215 }
216 }
217 for (int j = 0; j < _dTables.Count; j++)
218 {
219 foreach (DataRow row in ((DataTable)_dTables[j]).Rows)
220 {
221 if (row.RowState != DataRowState.Deleted)
222 {
223 int nestedParentCount = row.GetNestedParentCount();
224 if (nestedParentCount == 0)
225 {
226 XmlDataRowWriter(row, ((DataTable)_dTables[j]).EncodedTableName);
227 }
228 else if (nestedParentCount > 1)
229 {
231 throw ExceptionBuilder.MultipleParentRows((dataTable.Namespace.Length == 0) ? dataTable.TableName : (dataTable.Namespace + dataTable.TableName));
232 }
233 }
234 }
235 }
236 if (flag)
237 {
239 }
240 _xmlw.Flush();
241 }
242
244 {
246 foreach (DataRelation childRelation in row.Table.ChildRelations)
247 {
248 if (childRelation.Nested)
249 {
251 }
252 }
253 return arrayList;
254 }
255
256 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
258 {
259 string prefix = ((row.Table.Namespace.Length == 0) ? "" : row.Table.Prefix);
261 if (_isDiffgram)
262 {
263 _xmlw.WriteAttributeString("diffgr", "id", "urn:schemas-microsoft-com:xml-diffgram-v1", row.Table.TableName + row.rowID.ToString(CultureInfo.InvariantCulture));
264 _xmlw.WriteAttributeString("msdata", "rowOrder", "urn:schemas-microsoft-com:xml-msdata", _rowsOrder[row].ToString());
265 if (row.RowState == DataRowState.Added)
266 {
267 _xmlw.WriteAttributeString("diffgr", "hasChanges", "urn:schemas-microsoft-com:xml-diffgram-v1", "inserted");
268 }
269 if (row.RowState == DataRowState.Modified)
270 {
271 _xmlw.WriteAttributeString("diffgr", "hasChanges", "urn:schemas-microsoft-com:xml-diffgram-v1", "modified");
272 }
273 if (RowHasErrors(row))
274 {
275 _xmlw.WriteAttributeString("diffgr", "hasErrors", "urn:schemas-microsoft-com:xml-diffgram-v1", "true");
276 }
277 }
278 foreach (DataColumn column in row.Table.Columns)
279 {
280 if (column._columnMapping == MappingType.Attribute)
281 {
282 object obj = row[column];
283 string prefix2 = ((column.Namespace.Length == 0) ? "" : column.Prefix);
284 if (obj != DBNull.Value && (!column.ImplementsINullable || !DataStorage.IsObjectSqlNull(obj)))
285 {
287 _xmlw.WriteAttributeString(prefix2, column.EncodedColumnName, column.Namespace, column.ConvertObjectToXml(obj));
288 }
289 }
290 if (_isDiffgram && column._columnMapping == MappingType.Hidden)
291 {
292 object obj = row[column];
293 if (obj != DBNull.Value && (!column.ImplementsINullable || !DataStorage.IsObjectSqlNull(obj)))
294 {
296 _xmlw.WriteAttributeString("msdata", "hidden" + column.EncodedColumnName, "urn:schemas-microsoft-com:xml-msdata", column.ConvertObjectToXml(obj));
297 }
298 }
299 }
300 foreach (DataColumn column2 in row.Table.Columns)
301 {
302 if (column2._columnMapping == MappingType.Hidden)
303 {
304 continue;
305 }
306 object obj = row[column2];
307 string prefix3 = ((column2.Namespace.Length == 0) ? "" : column2.Prefix);
308 bool flag = true;
309 if ((obj == DBNull.Value || (column2.ImplementsINullable && DataStorage.IsObjectSqlNull(obj))) && column2.ColumnMapping == MappingType.SimpleContent)
310 {
311 _xmlw.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
312 }
313 if (obj == DBNull.Value || (column2.ImplementsINullable && DataStorage.IsObjectSqlNull(obj)) || column2._columnMapping == MappingType.Attribute)
314 {
315 continue;
316 }
317 if (column2._columnMapping != MappingType.SimpleContent && (!column2.IsCustomType || !column2.IsValueCustomTypeInstance(obj) || typeof(IXmlSerializable).IsAssignableFrom(obj.GetType())))
318 {
319 _xmlw.WriteStartElement(prefix3, column2.EncodedColumnName, column2.Namespace);
320 flag = false;
321 }
322 Type type = obj.GetType();
323 if (!column2.IsCustomType)
324 {
325 if ((type == typeof(char) || type == typeof(string)) && PreserveSpace(obj))
326 {
327 _xmlw.WriteAttributeString("xml", "space", "http://www.w3.org/XML/1998/namespace", "preserve");
328 }
329 _xmlw.WriteString(column2.ConvertObjectToXml(obj));
330 }
331 else if (column2.IsValueCustomTypeInstance(obj))
332 {
333 if (!flag && type != column2.DataType)
334 {
335 _xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", DataStorage.GetQualifiedName(type));
336 }
337 if (!flag)
338 {
339 column2.ConvertObjectToXml(obj, _xmlw, null);
340 }
341 else
342 {
343 if (obj.GetType() != column2.DataType)
344 {
345 throw ExceptionBuilder.PolymorphismNotSupported(type.AssemblyQualifiedName);
346 }
349 column2.ConvertObjectToXml(obj, _xmlw, xmlRootAttribute);
350 }
351 }
352 else
353 {
354 if (type == typeof(Type) || type == typeof(Guid) || type == typeof(char) || DataStorage.IsSqlType(type))
355 {
356 _xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", type.FullName);
357 }
358 else if (obj is Type)
359 {
360 _xmlw.WriteAttributeString("msdata", "InstanceType", "urn:schemas-microsoft-com:xml-msdata", "Type");
361 }
362 else
363 {
364 string value = "xs:" + XmlTreeGen.XmlDataTypeName(type);
365 _xmlw.WriteAttributeString("xsi", "type", "http://www.w3.org/2001/XMLSchema-instance", value);
366 _xmlw.WriteAttributeString("xs", "xmlns", "http://www.w3.org/2001/XMLSchema", value);
367 }
369 {
370 _xmlw.WriteString(column2.ConvertObjectToXml(obj));
371 }
372 else
373 {
374 column2.ConvertObjectToXml(obj, _xmlw, null);
375 }
376 }
377 if (column2._columnMapping != MappingType.SimpleContent && !flag)
378 {
380 }
381 }
382 if (_ds != null)
383 {
385 {
387 foreach (DataRow row2 in childRows)
388 {
389 XmlDataRowWriter(row2, nestedChildRelation.ChildTable.EncodedTableName);
390 }
391 }
392 }
394 }
395
396 internal static bool PreserveSpace(object value)
397 {
398 string text = value.ToString();
399 if (text.Length == 0)
400 {
401 return false;
402 }
403 for (int i = 0; i < text.Length; i++)
404 {
405 if (!char.IsWhiteSpace(text, i))
406 {
407 return false;
408 }
409 }
410 return true;
411 }
412}
virtual int Add(object? value)
virtual bool Contains(object? item)
static readonly DBNull Value
Definition DBNull.cs:8
static string GetQualifiedName(Type type)
static bool IsObjectSqlNull(object value)
static bool IsSqlType(StorageType storageType)
DataRow[] GetChildRows(string? relationName)
Definition DataRow.cs:672
DataTable[] TopLevelTables()
Definition DataSet.cs:2640
DataRowCollection Rows
Definition DataTable.cs:701
DataColumnCollection Columns
Definition DataTable.cs:327
static XmlWriter CreateWriter(XmlWriter xw)
static Exception MultipleParentRows(string tableQName)
static Exception PolymorphismNotSupported(string typeName)
static bool PreserveSpace(object value)
void XmlDataRowWriter(DataRow row, string encodedTableName)
void SaveDiffgramData(XmlWriter xw, Hashtable rowsOrder)
readonly DataTable[] _topLevelTables
static bool RowHasErrors(DataRow row)
void Save(XmlWriter xw, bool writeSchema)
XmlDataTreeWriter(DataTable dt, bool writeHierarchy)
void CreateTablesHierarchy(DataTable dt)
ArrayList GetNestedChildRelations(DataRow row)
static void ValidateColumnMapping(Type columnType)
static string XmlDataTypeName(Type type)
void Save(DataSet ds, XmlWriter xw)
static CultureInfo InvariantCulture
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
static ? string EncodeLocalName(string? name)
Definition XmlConvert.cs:49
void WriteString(string? text)
void WriteAttributeString(string localName, string? ns, string? value)
Definition XmlWriter.cs:46
void WriteStartElement(string localName, string? ns)
Definition XmlWriter.cs:30