internal void Save(XmlWriter xw, bool writeSchema)
{
_xmlw = DataTextWriter.CreateWriter(xw);
int countTopTable = _topLevelTables.Length;
bool fWriteDSElement = true;
string prefix = (_ds != null) ? ((_ds.Namespace.Length == 0) ? "" : _ds.Prefix) : ((_dt.Namespace.Length == 0) ? "" : _dt.Prefix);
if (!writeSchema && _ds != null && _ds._fTopLevelTable && countTopTable == 1)
{
if (_ds.TopLevelTables()[0].Rows.Count == 1)
fWriteDSElement = false;
}
if (fWriteDSElement)
{
if (_ds == null)
{
_xmlw.WriteStartElement(prefix, Keywords.DOCUMENTELEMENT, _dt.Namespace);
}
else
{
if (_ds.DataSetName == null || _ds.DataSetName.Length == 0)
_xmlw.WriteStartElement(prefix, Keywords.DOCUMENTELEMENT, _ds.Namespace);
else
_xmlw.WriteStartElement(prefix, XmlConvert.EncodeLocalName(_ds.DataSetName), _ds.Namespace);
}
for (int i = 0; i < _dTables.Count; i++)
{
if (((DataTable)_dTables[i])._xmlText != null)
{
_xmlw.WriteAttributeString(Keywords.XMLNS, Keywords.XSI, Keywords.XSD_XMLNS_NS, Keywords.XSINS);
break;
}
}
if (writeSchema)
{
if (!_fFromTable)
{
new XmlTreeGen(SchemaFormat.Public).Save(_ds, _xmlw);
}
else
{
new XmlTreeGen(SchemaFormat.Public).Save(null, _dt, _xmlw, _writeHierarchy);
}
}
}
for (int i = 0; i < _dTables.Count; i++)
{
foreach (DataRow row in ((DataTable)_dTables[i]).Rows)
{
if (row.RowState == DataRowState.Deleted)
continue;
int parentRowCount = row.GetNestedParentCount();
if (parentRowCount == 0)
{
XmlDataRowWriter(row, ((DataTable)_dTables[i]).EncodedTableName);
}
else if (parentRowCount > 1)
{
DataTable dt = (DataTable)_dTables[i];
throw ExceptionBuilder.MultipleParentRows(dt.Namespace.Length == 0 ? dt.TableName : (dt.Namespace + dt.TableName));
// At all times a nested row can only have 0 or 1 parents, never more than 1
}
}
}
if (fWriteDSElement)
_xmlw.WriteEndElement();
_xmlw.Flush();
}