internal XmlDataTreeWriter(DataSet ds, DataTable dt) { this._dTables = new ArrayList(); this._ds = ds; this._dt = dt; this._dTables.Add(dt); this.topLevelTables = ds.TopLevelTables(); }
internal XmlDataTreeWriter(DataSet ds) { this._dTables = new ArrayList(); this._ds = ds; this.topLevelTables = ds.TopLevelTables(); foreach (DataTable table in ds.Tables) { this._dTables.Add(table); } }
internal void SchemaTree(XmlDocument xd, XmlWriter xmlWriter, DataSet ds, DataTable dt, bool writeHierarchy) { ConstraintNames = new ArrayList(); autogenerated = new Hashtable(); bool genSecondary = filePath != null; //null non-file based streams. dsElement = xd.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_ELEMENT, Keywords.XSDNS); DataTable [] top; bool fFlat = false; DataTable _dt = dt; if (ds != null) { _ds = ds; foreach(DataTable table in ds.Tables) { _tables.Add(table); } } else { if (dt.DataSet != null) { // preserve datatable's dataset to use for xml // if null it would write out document element instead of dt.DataSet.DataSetName _ds = dt.DataSet; } _tables.Add(dt); if (writeHierarchy) { CreateTablesHierarchy(dt); } } _dc = xd; namespaces = new Hashtable(); prefixes = new Hashtable(); XmlElement rootSchema = xd.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_SCHEMA, Keywords.XSDNS); _sRoot = rootSchema; // Need to writeid attribute on schema, as webservice relys on it for typeddataset deserialization // to get class name if (_ds != null) { rootSchema.SetAttribute(Keywords.XSDID, XmlConvert.EncodeLocalName(_ds.DataSetName)); } else { rootSchema.SetAttribute(Keywords.XSDID, XmlConvert.EncodeLocalName("NewDataSet")); } if (_ds != null) { WriteSchemaRoot(xd, rootSchema, _ds.Namespace); } else { WriteSchemaRoot(xd, rootSchema, _dt.Namespace); } // register the root element and associated NS if (schFormat == SchemaFormat.Remoting) { if (_ds != null) { namespaces[_ds.Namespace] = rootSchema; } else { namespaces[_dt.Namespace] = rootSchema; } } if (schFormat != SchemaFormat.Remoting) { if (_ds != null) { namespaces[_ds.Namespace] = rootSchema; if (_ds.Namespace.Length == 0) prefixes[_ds.Namespace] = null; else { // generate a prefix for the dataset schema itself. rootSchema.SetAttribute(Keywords.XMLNS_MSTNS, _ds.Namespace ); prefixes[_ds.Namespace] = "mstns"; } } } // Generate all the constraint names if (ds != null) GenerateConstraintNames(ds); else GenerateConstraintNames(_tables); // Setup AutoGenerated table if (schFormat != SchemaFormat.Remoting) { if (ds != null) { SetupAutoGenerated(ds); } else { SetupAutoGenerated(_tables); } } // // Output all top level elements, which will recursively invoke to other tables. // top = ((ds != null) ? ds.TopLevelTables(true) : CreateToplevelTables()); if (top.Length == 0 || schFormat == SchemaFormat.WebServiceSkipSchema || schFormat == SchemaFormat.RemotingSkipSchema) { // return an empty schema for now. // probably we need to throw an exception FillDataSetElement(xd, ds, dt); rootSchema.AppendChild(dsElement); AddXdoProperties(_ds, dsElement, xd ); AddExtendedProperties(ds.extendedProperties, dsElement); xd.AppendChild(rootSchema); xd.Save(xmlWriter); xmlWriter.Flush(); return ; // rootSchema content has already been pushed to xmlWriter } // if (schFormat != SchemaFormat.WebService && namespaces.Count > 1 && !genSecondary) { // rootSchema.SetAttribute(Keywords.MSD_FRAGMENTCOUNT, Keywords.MSDNS, namespaces.Count.ToString()); // } // Fill out dataset element XmlElement dsCompositor = FillDataSetElement(xd, ds, dt); constraintSeparator = xd.CreateElement(Keywords.XSD_PREFIX, "SHOULDNOTBEHERE", Keywords.XSDNS); dsElement.AppendChild(constraintSeparator); // DataSet properties if (_ds != null) { AddXdoProperties(_ds, dsElement, xd ); AddExtendedProperties(_ds.extendedProperties, dsElement); } for (int i = 0; i < top.Length; i++) { XmlElement el = HandleTable(top[i], xd, rootSchema); if (((_ds != null )&& (_ds.Namespace == top[i].Namespace)) || Common.ADP.IsEmpty(top[i].Namespace) || (schFormat == SchemaFormat.Remoting)) { bool fNestedInDataset = top[i].fNestedInDataset; if (((_ds != null )&& (_ds.Namespace.Length != 0)) && Common.ADP.IsEmpty(top[i].Namespace)) { fNestedInDataset = true; } // what if dt has two nested relation , one self nested , the other with dtParent if (top[i].SelfNested) { // regarding above check : is it selfnested! fNestedInDataset = false; } if (top[i].NestedParentsCount > 1) { // if it has multiple parents, it should be global fNestedInDataset = false; } if(fNestedInDataset) { //deal with maxOccurs properly if (top[i].MinOccurs != 1) { el.SetAttribute(Keywords.MINOCCURS, top[i].MinOccurs.ToString(CultureInfo.InvariantCulture)); } if (top[i].MaxOccurs == -1){ el.SetAttribute(Keywords.MAXOCCURS, Keywords.ZERO_OR_MORE); } else if (top[i].MaxOccurs != 1){ el.SetAttribute(Keywords.MAXOCCURS, top[i].MaxOccurs.ToString(CultureInfo.InvariantCulture)); } } if (!fNestedInDataset) { rootSchema.AppendChild(el); XmlElement node = xd.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_ELEMENT, Keywords.XSDNS); if ((_ds != null && _ds.Namespace == top[i].Namespace) || Common.ADP.IsEmpty(top[i].Namespace) || (schFormat == SchemaFormat.Remoting)) node.SetAttribute(Keywords.REF, top[i].EncodedTableName); else node.SetAttribute(Keywords.REF, ((string)prefixes[top[i].Namespace])+':'+top[i].EncodedTableName); dsCompositor.AppendChild(node); } else dsCompositor.AppendChild(el); } else { AppendChildWithoutRef(rootSchema, top[i].Namespace, el, Keywords.XSD_ELEMENT); XmlElement node = xd.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_ELEMENT, Keywords.XSDNS); node.SetAttribute(Keywords.REF, ((string)prefixes[top[i].Namespace])+':'+top[i].EncodedTableName); dsCompositor.AppendChild(node); } } dsElement.RemoveChild(constraintSeparator); rootSchema.AppendChild(dsElement); // Output all non-heirarchical relations without constraints DataRelation [] rels = new DataRelation[0]; if (ds != null && _tables.Count> 0) { // we need to make sure we want to write relation just for tables in list rels = new DataRelation[ds.Relations.Count]; for (int i = 0 ; i < ds.Relations.Count ; i++) { rels[i] = ds.Relations[i]; } } else if (writeHierarchy && _tables.Count > 0 ) { CreateRelations((DataTable)_tables[0]); rels = new DataRelation[_relations.Count]; _relations.CopyTo(rels, 0); } XmlElement nodeAnn = null; XmlElement nodeApp = null; for (int i = 0; i < rels.Length; ++i) { DataRelation rel = rels[i]; if (!rel.Nested || fFlat) { if (rel.ChildKeyConstraint == null) { if (nodeAnn == null) { nodeAnn = xd.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_ANNOTATION, Keywords.XSDNS); rootSchema.AppendChild(nodeAnn); nodeApp = xd.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_APPINFO, Keywords.XSDNS); nodeAnn.AppendChild(nodeApp); } Debug.Assert(nodeApp != null, "Need to create <application..> node first."); nodeApp.AppendChild(HandleRelation(rel, xd)); } } } XmlComment comment = null; bool isMultipleNamespaceAndStreamingWriter = (namespaces.Count > 1 && !genSecondary); if (schFormat != SchemaFormat.Remoting && schFormat != SchemaFormat.RemotingSkipSchema) { // complete processing of rootSchema foreach (string ns in namespaces.Keys) { if (ns == ((_ds != null) ? _ds.Namespace : _dt.Namespace) || Common.ADP.IsEmpty(ns)) { continue; } XmlElement _import = xd.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_IMPORT, Keywords.XSDNS); _import.SetAttribute(Keywords.XSD_NAMESPACE, ns); if ( schFormat != SchemaFormat.WebService && !isMultipleNamespaceAndStreamingWriter) { _import.SetAttribute(Keywords.XSD_SCHEMALOCATION, fileName + "_" + prefixes[ns] + ".xsd"); } ((XmlNode)rootSchema).PrependChild((XmlNode)_import); } if (schFormat != SchemaFormat.WebService && isMultipleNamespaceAndStreamingWriter) { rootSchema.SetAttribute(Keywords.MSD_FRAGMENTCOUNT, Keywords.MSDNS, namespaces.Count.ToString(CultureInfo.InvariantCulture)); } // Post rootSchema content to xmlWriter. xd.AppendChild(rootSchema); // KB if (schFormat != SchemaFormat.WebService && isMultipleNamespaceAndStreamingWriter) { xd.WriteTo(xmlWriter); } else { xd.Save(xmlWriter); } xd.RemoveChild(rootSchema); //KB foreach(string ns in namespaces.Keys) { if (ns == ((_ds != null)?_ds.Namespace:_dt.Namespace) || Common.ADP.IsEmpty(ns)) { continue; } XmlWriter xw = null; if (!genSecondary) { xw = xmlWriter; } else { xw = new XmlTextWriter(filePath + fileName + "_" + prefixes[ns] + ".xsd", null); } try { if (genSecondary) { if (xw is XmlTextWriter) { ((XmlTextWriter)xw).Formatting = Formatting.Indented; } xw.WriteStartDocument(true); } XmlElement tNode = (XmlElement) namespaces[ns] ; _dc.AppendChild( tNode ); foreach(string imp_ns in namespaces.Keys) { if (ns == imp_ns) { continue; // don't write out yourself } string prefix = (string) prefixes[imp_ns]; if (prefix == null) { // only for dataset.Namespace == empty continue; // do nothing } tNode.SetAttribute("xmlns:"+prefix, imp_ns); XmlElement _import2 = _dc.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_IMPORT, Keywords.XSDNS); _import2.SetAttribute(Keywords.XSD_NAMESPACE, imp_ns); if ( schFormat != SchemaFormat.WebService && !isMultipleNamespaceAndStreamingWriter) { if (imp_ns == ((_ds != null)?_ds.Namespace:_dt.Namespace)) _import2.SetAttribute(Keywords.XSD_SCHEMALOCATION, fileName + fileExt); // for the dataset namespace don't append anything else _import2.SetAttribute(Keywords.XSD_SCHEMALOCATION, fileName + "_" + prefix +".xsd"); } ((XmlNode)tNode).PrependChild((XmlNode)_import2); } if (schFormat != SchemaFormat.WebService && isMultipleNamespaceAndStreamingWriter) { _dc.WriteTo(xw); } else { _dc.Save(xw); } _dc.RemoveChild( tNode ); if (genSecondary) { xw.WriteEndDocument(); } } finally { if (genSecondary) { xw.Close(); } } } } else { xd.AppendChild(rootSchema); xd.Save(xmlWriter); } if (comment != null) { ((XmlNode)rootSchema).PrependChild((XmlNode)comment); } if (!genSecondary) { xmlWriter.Flush(); } return;// rootSchema; }
internal XmlDataTreeWriter(DataSet ds, DataTable dt) { // need to modify this also _ds = ds; _dt = dt; _dTables.Add(dt); topLevelTables = ds.TopLevelTables(); }
internal XmlDataTreeWriter(DataSet ds) { _ds = ds; topLevelTables = ds.TopLevelTables(); foreach(DataTable table in ds.Tables) { _dTables.Add(table); } }
internal void SchemaTree(XmlDocument xd, XmlWriter xmlWriter, DataSet ds, DataTable dt, bool writeHierarchy) { this.ConstraintNames = new ArrayList(); this.autogenerated = new Hashtable(); bool flag = this.filePath != null; this.dsElement = xd.CreateElement("xs", "element", "http://www.w3.org/2001/XMLSchema"); bool flag4 = false; DataTable table = dt; if (ds != null) { this._ds = ds; foreach (DataTable table2 in ds.Tables) { this._tables.Add(table2); } } else { if (dt.DataSet != null) { this._ds = dt.DataSet; } this._tables.Add(dt); if (writeHierarchy) { this.CreateTablesHierarchy(dt); } } this._dc = xd; this.namespaces = new Hashtable(); this.prefixes = new Hashtable(); XmlElement rootSchema = xd.CreateElement("xs", "schema", "http://www.w3.org/2001/XMLSchema"); this._sRoot = rootSchema; if (this._ds != null) { rootSchema.SetAttribute("id", XmlConvert.EncodeLocalName(this._ds.DataSetName)); } else { rootSchema.SetAttribute("id", XmlConvert.EncodeLocalName("NewDataSet")); } if (this._ds != null) { this.WriteSchemaRoot(xd, rootSchema, this._ds.Namespace); } else { this.WriteSchemaRoot(xd, rootSchema, table.Namespace); } if (this.schFormat == SchemaFormat.Remoting) { if (this._ds != null) { this.namespaces[this._ds.Namespace] = rootSchema; } else { this.namespaces[table.Namespace] = rootSchema; } } if ((this.schFormat != SchemaFormat.Remoting) && (this._ds != null)) { this.namespaces[this._ds.Namespace] = rootSchema; if (this._ds.Namespace.Length == 0) { this.prefixes[this._ds.Namespace] = null; } else { rootSchema.SetAttribute("xmlns:mstns", this._ds.Namespace); this.prefixes[this._ds.Namespace] = "mstns"; } } if (ds != null) { this.GenerateConstraintNames(ds); } else { this.GenerateConstraintNames(this._tables); } if (this.schFormat != SchemaFormat.Remoting) { if (ds != null) { this.SetupAutoGenerated(ds); } else { this.SetupAutoGenerated(this._tables); } } DataTable[] tableArray = (ds != null) ? ds.TopLevelTables(true) : this.CreateToplevelTables(); if (((tableArray.Length == 0) || (this.schFormat == SchemaFormat.WebServiceSkipSchema)) || (this.schFormat == SchemaFormat.RemotingSkipSchema)) { this.FillDataSetElement(xd, ds, dt); rootSchema.AppendChild(this.dsElement); this.AddXdoProperties(this._ds, this.dsElement, xd); AddExtendedProperties(ds.extendedProperties, this.dsElement); xd.AppendChild(rootSchema); xd.Save(xmlWriter); xmlWriter.Flush(); } else { XmlElement element9 = this.FillDataSetElement(xd, ds, dt); this.constraintSeparator = xd.CreateElement("xs", "SHOULDNOTBEHERE", "http://www.w3.org/2001/XMLSchema"); this.dsElement.AppendChild(this.constraintSeparator); if (this._ds != null) { this.AddXdoProperties(this._ds, this.dsElement, xd); AddExtendedProperties(this._ds.extendedProperties, this.dsElement); } for (int i = 0; i < tableArray.Length; i++) { XmlElement element2 = this.HandleTable(tableArray[i], xd, rootSchema); if (((this._ds != null) && (this._ds.Namespace == tableArray[i].Namespace)) || (ADP.IsEmpty(tableArray[i].Namespace) || (this.schFormat == SchemaFormat.Remoting))) { bool fNestedInDataset = tableArray[i].fNestedInDataset; if (((this._ds != null) && (this._ds.Namespace.Length != 0)) && ADP.IsEmpty(tableArray[i].Namespace)) { fNestedInDataset = true; } if (tableArray[i].SelfNested) { fNestedInDataset = false; } if (tableArray[i].NestedParentsCount > 1) { fNestedInDataset = false; } if (fNestedInDataset) { if (tableArray[i].MinOccurs != 1M) { element2.SetAttribute("minOccurs", tableArray[i].MinOccurs.ToString(CultureInfo.InvariantCulture)); } if (tableArray[i].MaxOccurs == -1M) { element2.SetAttribute("maxOccurs", "unbounded"); } else if (tableArray[i].MaxOccurs != 1M) { element2.SetAttribute("maxOccurs", tableArray[i].MaxOccurs.ToString(CultureInfo.InvariantCulture)); } } if (!fNestedInDataset) { rootSchema.AppendChild(element2); XmlElement element8 = xd.CreateElement("xs", "element", "http://www.w3.org/2001/XMLSchema"); if (((this._ds != null) && (this._ds.Namespace == tableArray[i].Namespace)) || (ADP.IsEmpty(tableArray[i].Namespace) || (this.schFormat == SchemaFormat.Remoting))) { element8.SetAttribute("ref", tableArray[i].EncodedTableName); } else { element8.SetAttribute("ref", ((string) this.prefixes[tableArray[i].Namespace]) + ':' + tableArray[i].EncodedTableName); } element9.AppendChild(element8); } else { element9.AppendChild(element2); } } else { this.AppendChildWithoutRef(rootSchema, tableArray[i].Namespace, element2, "element"); XmlElement element10 = xd.CreateElement("xs", "element", "http://www.w3.org/2001/XMLSchema"); element10.SetAttribute("ref", ((string) this.prefixes[tableArray[i].Namespace]) + ':' + tableArray[i].EncodedTableName); element9.AppendChild(element10); } } this.dsElement.RemoveChild(this.constraintSeparator); rootSchema.AppendChild(this.dsElement); DataRelation[] array = new DataRelation[0]; if ((ds != null) && (this._tables.Count > 0)) { array = new DataRelation[ds.Relations.Count]; for (int k = 0; k < ds.Relations.Count; k++) { array[k] = ds.Relations[k]; } } else if (writeHierarchy && (this._tables.Count > 0)) { this.CreateRelations((DataTable) this._tables[0]); array = new DataRelation[this._relations.Count]; this._relations.CopyTo(array, 0); } XmlElement newChild = null; XmlElement element7 = null; for (int j = 0; j < array.Length; j++) { DataRelation rel = array[j]; if ((!rel.Nested || flag4) && (rel.ChildKeyConstraint == null)) { if (newChild == null) { newChild = xd.CreateElement("xs", "annotation", "http://www.w3.org/2001/XMLSchema"); rootSchema.AppendChild(newChild); element7 = xd.CreateElement("xs", "appinfo", "http://www.w3.org/2001/XMLSchema"); newChild.AppendChild(element7); } element7.AppendChild(this.HandleRelation(rel, xd)); } } XmlComment comment = null; bool flag2 = (this.namespaces.Count > 1) && !flag; if ((this.schFormat != SchemaFormat.Remoting) && (this.schFormat != SchemaFormat.RemotingSkipSchema)) { foreach (string str3 in this.namespaces.Keys) { if ((str3 != ((this._ds != null) ? this._ds.Namespace : table.Namespace)) && !ADP.IsEmpty(str3)) { XmlElement element6 = xd.CreateElement("xs", "import", "http://www.w3.org/2001/XMLSchema"); element6.SetAttribute("namespace", str3); if ((this.schFormat != SchemaFormat.WebService) && !flag2) { element6.SetAttribute("schemaLocation", string.Concat(new object[] { this.fileName, "_", this.prefixes[str3], ".xsd" })); } rootSchema.PrependChild(element6); } } if ((this.schFormat != SchemaFormat.WebService) && flag2) { rootSchema.SetAttribute("schemafragmentcount", "urn:schemas-microsoft-com:xml-msdata", this.namespaces.Count.ToString(CultureInfo.InvariantCulture)); } xd.AppendChild(rootSchema); if ((this.schFormat != SchemaFormat.WebService) && flag2) { xd.WriteTo(xmlWriter); } else { xd.Save(xmlWriter); } xd.RemoveChild(rootSchema); foreach (string str2 in this.namespaces.Keys) { if ((str2 != ((this._ds != null) ? this._ds.Namespace : table.Namespace)) && !ADP.IsEmpty(str2)) { XmlWriter w = null; if (!flag) { w = xmlWriter; } else { w = new XmlTextWriter(string.Concat(new object[] { this.filePath, this.fileName, "_", this.prefixes[str2], ".xsd" }), null); } try { if (flag) { if (w is XmlTextWriter) { ((XmlTextWriter) w).Formatting = Formatting.Indented; } w.WriteStartDocument(true); } XmlElement element4 = (XmlElement) this.namespaces[str2]; this._dc.AppendChild(element4); foreach (string str in this.namespaces.Keys) { if (str2 != str) { string str4 = (string) this.prefixes[str]; if (str4 != null) { element4.SetAttribute("xmlns:" + str4, str); XmlElement element3 = this._dc.CreateElement("xs", "import", "http://www.w3.org/2001/XMLSchema"); element3.SetAttribute("namespace", str); if ((this.schFormat != SchemaFormat.WebService) && !flag2) { if (str == ((this._ds != null) ? this._ds.Namespace : table.Namespace)) { element3.SetAttribute("schemaLocation", this.fileName + this.fileExt); } else { element3.SetAttribute("schemaLocation", this.fileName + "_" + str4 + ".xsd"); } } element4.PrependChild(element3); } } } if ((this.schFormat != SchemaFormat.WebService) && flag2) { this._dc.WriteTo(w); } else { this._dc.Save(w); } this._dc.RemoveChild(element4); if (flag) { w.WriteEndDocument(); } } finally { if (flag) { w.Close(); } } } } } else { xd.AppendChild(rootSchema); xd.Save(xmlWriter); } if (comment != null) { rootSchema.PrependChild(comment); } if (!flag) { xmlWriter.Flush(); } } }