private void ReadXmlDiffgram(XmlReader reader) { // fill correctly
            int d = reader.Depth;
            bool fEnforce = this.EnforceConstraints;
            this.EnforceConstraints =false;
            DataTable newDt;
            bool isEmpty;

            if (this.Rows.Count == 0) {
                isEmpty = true;
                newDt = this;
            }
            else {
                isEmpty = false;
                newDt = this.Clone();
                newDt.EnforceConstraints = false;
            }

            newDt.Rows.nullInList = 0;

            reader.MoveToContent();

            if ((reader.LocalName != Keywords.DIFFGRAM) && (reader.NamespaceURI != Keywords.DFFNS))
                return;
            reader.Read();
            if (reader.NodeType == XmlNodeType.Whitespace) {
                MoveToElement(reader, reader.Depth - 1 /*iCurrentDepth*/); // skip over whitespaces.
            }

            newDt.fInLoadDiffgram = true;

            if (reader.Depth > d) {
                if ((reader.NamespaceURI != Keywords.DFFNS) && (reader.NamespaceURI != Keywords.MSDNS)) {
                    //we should be inside the dataset part
                    XmlDocument xdoc = new XmlDocument();
                    XmlElement node = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                    reader.Read();
                    if (reader.Depth-1 > d) {
                        XmlDataLoader xmlload = new XmlDataLoader(newDt, false, node, false);
                        xmlload.isDiffgram = true; // turn on the special processing
                        xmlload.LoadData(reader);
                    }
                    ReadEndElement(reader);
                }
                if (((reader.LocalName == Keywords.SQL_BEFORE) && (reader.NamespaceURI == Keywords.DFFNS)) ||
                    ((reader.LocalName == Keywords.MSD_ERRORS) && (reader.NamespaceURI == Keywords.DFFNS)))

                {
                    //this will consume the changes and the errors part
                    XMLDiffLoader diffLoader = new XMLDiffLoader();
                    diffLoader.LoadDiffGram(newDt, reader);
                }

                // get to the closing diff tag
                while(reader.Depth > d) {
                    reader.Read();
                }
                // read the closing tag
                ReadEndElement(reader);
            }

            if (newDt.Rows.nullInList > 0)
                throw ExceptionBuilder.RowInsertMissing(newDt.TableName);


            newDt.fInLoadDiffgram = false;
            List<DataTable> tableList = new List<DataTable>();
            tableList.Add(this);
            CreateTableList(this, tableList);

// this is terrible, optimize it
            for (int i = 0; i < tableList.Count ; i++) {
                DataRelation[] relations = tableList[i].NestedParentRelations;
                foreach(DataRelation rel in relations) {
                    if (rel != null && rel.ParentTable == tableList[i]) {
                        foreach (DataRow r in tableList[i].Rows) {
                            foreach (DataRelation rel2 in relations) {
                                r.CheckForLoops(rel2);
                            }
                        }
                    }
                }
            }

            if (!isEmpty) {
                this.Merge(newDt);
            }
            this.EnforceConstraints = fEnforce;
        }
        internal XmlReadMode ReadXml(XmlReader reader, bool denyResolving)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.ReadXml|INFO> %d#, denyResolving=%d{bool}\n", ObjectID, denyResolving);
            try {

              DataTable.RowDiffIdUsageSection rowDiffIdUsage = new DataTable.RowDiffIdUsageSection();
              try {
                bool fDataFound = false;
                bool fSchemaFound = false;
                bool fDiffsFound = false;
                bool fIsXdr = false;
                int iCurrentDepth = -1;
                XmlReadMode ret = XmlReadMode.Auto;

                // clear the hashtable to avoid conflicts between diffgrams, SqlHotFix 782
                rowDiffIdUsage.Prepare(this);

                if (reader == null)
                    return ret;
                bool originalEnforceConstraint = false;
                if (this.DataSet != null) {
                    originalEnforceConstraint = this.DataSet.EnforceConstraints;
                    this.DataSet.EnforceConstraints = false;
                }
                else {
                    originalEnforceConstraint = this.EnforceConstraints;
                    this.EnforceConstraints = false;
                }

                if (reader is XmlTextReader)
                    ((XmlTextReader) reader).WhitespaceHandling = WhitespaceHandling.Significant;

                XmlDocument xdoc = new XmlDocument(); // we may need this to infer the schema
                XmlDataLoader xmlload = null;


                reader.MoveToContent();
                if (Columns.Count == 0) {
                    if (IsEmptyXml(reader)) {
                        reader.Read();
                        return ret;
                    }
                }

                if (reader.NodeType == XmlNodeType.Element) {
                    iCurrentDepth = reader.Depth;

                    if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                        if (Columns.Count == 0) {
                            if (reader.IsEmptyElement) {
                                reader.Read();
                                return XmlReadMode.DiffGram;
                            }
                            throw ExceptionBuilder.DataTableInferenceNotSupported();
                        }
                        this.ReadXmlDiffgram(reader);
                        // read the closing tag of the current element
                        ReadEndElement(reader);

                        RestoreConstraint(originalEnforceConstraint);
                        return XmlReadMode.DiffGram;
                    }

                    // if reader points to the schema load it
                    if (reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI==Keywords.XDRNS) {
                        // load XDR schema and exit
                        ReadXDRSchema(reader);

                        RestoreConstraint(originalEnforceConstraint);
                        return XmlReadMode.ReadSchema; //since the top level element is a schema return
                    }

                    if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI==Keywords.XSDNS) {
                        // load XSD schema and exit
                        ReadXmlSchema(reader, denyResolving);
                        RestoreConstraint(originalEnforceConstraint);
                        return XmlReadMode.ReadSchema; //since the top level element is a schema return
                    }

                    if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal)) {
                        if (this.DataSet != null) { // we should not throw for constraint, we already will throw for unsupported schema, so restore enforce cost, but not via property
                            this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                        }
                        else {
                            this.enforceConstraints = originalEnforceConstraint;
                        }

                        throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);
                    }

                    // now either the top level node is a table and we load it through dataReader...

                    // ... or backup the top node and all its attributes because we may need to InferSchema
                    XmlElement topNode = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                    if (reader.HasAttributes) {
                        int attrCount = reader.AttributeCount;
                        for (int i=0;i<attrCount;i++) {
                            reader.MoveToAttribute(i);
                            if (reader.NamespaceURI.Equals(Keywords.XSD_XMLNS_NS))
                                topNode.SetAttribute(reader.Name, reader.GetAttribute(i));
                            else {
                                XmlAttribute attr = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                                attr.Prefix = reader.Prefix;
                                attr.Value = reader.GetAttribute(i);
                            }
                        }
                    }
                    reader.Read();

                    while(MoveToElement(reader, iCurrentDepth)) {

                        if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                            this.ReadXmlDiffgram(reader);
                            // read the closing tag of the current element
                            ReadEndElement(reader);
                            RestoreConstraint(originalEnforceConstraint);
                            return XmlReadMode.DiffGram;
                        }

                        // if reader points to the schema load it...


                        if (!fSchemaFound && !fDataFound && reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI==Keywords.XDRNS) {
                            // load XDR schema and exit
                            ReadXDRSchema(reader);
                            fSchemaFound = true;
                            fIsXdr = true;
                            continue;
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI==Keywords.XSDNS) {
                            // load XSD schema and exit
                            ReadXmlSchema(reader, denyResolving);
                            fSchemaFound = true;
                            continue;
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))  {
                            if (this.DataSet != null) { // we should not throw for constraint, we already will throw for unsupported schema, so restore enforce cost, but not via property
                                this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                            }
                            else {
                                this.enforceConstraints = originalEnforceConstraint;
                            }
                            throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);
                        }

                        if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                            this.ReadXmlDiffgram(reader);
                            fDiffsFound = true;
                            ret = XmlReadMode.DiffGram;
                        }
                        else {
                            // we found data here
                            fDataFound = true;

                            if (!fSchemaFound && Columns.Count == 0) {
                                XmlNode node = xdoc.ReadNode(reader);
                                topNode.AppendChild(node);
                            }
                            else {
                                if (xmlload == null)
                                    xmlload = new XmlDataLoader(this, fIsXdr, topNode, false);
                                xmlload.LoadData(reader);
                                if (fSchemaFound)
                                    ret = XmlReadMode.ReadSchema;
                                else
                                    ret = XmlReadMode.IgnoreSchema;
                            }
                        }

                    }
                    // read the closing tag of the current element
                    ReadEndElement(reader);

                    // now top node contains the data part
                    xdoc.AppendChild(topNode);

                    if (!fSchemaFound && Columns.Count == 0) {
                        if (IsEmptyXml(reader)) {
                            reader.Read();
                            return ret;
                        }
                        throw ExceptionBuilder.DataTableInferenceNotSupported();
                    }

                    if (xmlload == null)
                        xmlload = new XmlDataLoader(this, fIsXdr, false);

                    // so we InferSchema
                    if (!fDiffsFound) {// we need to add support for inference here
                    }
                }
                RestoreConstraint(originalEnforceConstraint);
                return ret;
              }
              finally {
                rowDiffIdUsage.Cleanup();
              }
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }
        }
        internal XmlReadMode ReadXml(XmlReader reader, XmlReadMode mode, bool denyResolving)
        {
            DataTable.RowDiffIdUsageSection rowDiffIdUsage = new DataTable.RowDiffIdUsageSection();
            try {
                bool fSchemaFound = false;
                bool fDataFound = false;
                bool fIsXdr = false;
                int iCurrentDepth = -1;
                XmlReadMode ret = mode;

                // Dev11 904428: prepare and cleanup rowDiffId hashtable
                rowDiffIdUsage.Prepare(this);

                if (reader == null)
                    return ret;

                bool originalEnforceConstraint  = false;
                if (this.DataSet != null) {
                    originalEnforceConstraint  = this.DataSet.EnforceConstraints;
                    this.DataSet.EnforceConstraints = false;
                }
                else {
                    originalEnforceConstraint  = this.EnforceConstraints;
                    this.EnforceConstraints = false;
                }

                if (reader is XmlTextReader)
                    ((XmlTextReader) reader).WhitespaceHandling = WhitespaceHandling.Significant;

                XmlDocument xdoc = new XmlDocument(); // we may need this to infer the schema

                if ((mode != XmlReadMode.Fragment) && (reader.NodeType == XmlNodeType.Element))
                    iCurrentDepth = reader.Depth;

                reader.MoveToContent();
                if (Columns.Count == 0) {
                    if (IsEmptyXml(reader)) {
                        reader.Read();
                        return ret;
                    }
                }

                XmlDataLoader xmlload = null;

                if (reader.NodeType == XmlNodeType.Element) {
                    XmlElement topNode = null;
                    if (mode == XmlReadMode.Fragment) {
                        xdoc.AppendChild(xdoc.CreateElement("ds_sqlXmlWraPPeR"));
                        topNode = xdoc.DocumentElement;
                    }
                    else { //handle the top node
                        if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                            if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema)) {
                                if (Columns.Count == 0) {
                                    if(reader.IsEmptyElement) {
                                        reader.Read();
                                        return XmlReadMode.DiffGram;
                                    }
                                    throw ExceptionBuilder.DataTableInferenceNotSupported();
                                }
                                this.ReadXmlDiffgram(reader);
                                // read the closing tag of the current element
                                ReadEndElement(reader);
                            }
                            else {
                                reader.Skip();
                            }
                            RestoreConstraint(originalEnforceConstraint);
                            return ret;
                        }

                        if (reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI==Keywords.XDRNS) {
                            // load XDR schema and exit
                            if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema)) {
                                ReadXDRSchema(reader);
                            }
                            else {
                                reader.Skip();
                            }
                            RestoreConstraint(originalEnforceConstraint);
                            return ret; //since the top level element is a schema return
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI==Keywords.XSDNS) {
                            // load XSD schema and exit
                            if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))  {
                                ReadXmlSchema(reader, denyResolving);
                            }
                            else
                                reader.Skip();
                            RestoreConstraint(originalEnforceConstraint);
                            return ret; //since the top level element is a schema return
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))  {
                             if (this.DataSet != null) { // we should not throw for constraint, we already will throw for unsupported schema, so restore enforce cost, but not via property
                                 this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                             }
                             else {
                                this.enforceConstraints = originalEnforceConstraint;
                            }
                            throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);
                        }

                        // now either the top level node is a table and we load it through dataReader...
                        // ... or backup the top node and all its attributes
                        topNode = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                        if (reader.HasAttributes) {
                            int attrCount = reader.AttributeCount;
                            for (int i=0;i<attrCount;i++) {
                                reader.MoveToAttribute(i);
                                if (reader.NamespaceURI.Equals(Keywords.XSD_XMLNS_NS))
                                    topNode.SetAttribute(reader.Name, reader.GetAttribute(i));
                                else {
                                    XmlAttribute attr = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                                    attr.Prefix = reader.Prefix;
                                    attr.Value = reader.GetAttribute(i);
                                }
                            }
                        }
                        reader.Read();
                    }

                    while(MoveToElement(reader, iCurrentDepth)) {

                        if (reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI==Keywords.XDRNS) {
                            // load XDR schema
                       	    if (!fSchemaFound && !fDataFound && (mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))  {
                                ReadXDRSchema(reader);
                                fSchemaFound = true;
                                fIsXdr = true;
                            }
                            else {
                                reader.Skip();
                            }
                            continue;
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI==Keywords.XSDNS) {
                        // load XSD schema and exit
                            if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))  {
                                ReadXmlSchema(reader, denyResolving);
                                fSchemaFound = true;
                            }
                            else {
                                reader.Skip();
                            }
                            continue;
                        }

                        if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                            if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema)) {
                                if (Columns.Count == 0) {
                                    if(reader.IsEmptyElement) {
                                        reader.Read();
                                        return XmlReadMode.DiffGram;
                                    }
                                    throw ExceptionBuilder.DataTableInferenceNotSupported();
                                }
                                this.ReadXmlDiffgram(reader);
                                ret = XmlReadMode.DiffGram;
                            }
                            else {
                                reader.Skip();
                            }
                            continue;
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))  {
                            if (this.DataSet != null) { // we should not throw for constraint, we already will throw for unsupported schema, so restore enforce cost, but not via property
                                 this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                            }
                            else {
                                this.enforceConstraints = originalEnforceConstraint;
                            }
                            throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);
                        }

                        if (mode == XmlReadMode.DiffGram) {
                            reader.Skip();
                            continue; // we do not read data in diffgram mode
                        }

                        // if we are here we found some data
                        fDataFound = true;

                        if (mode == XmlReadMode.InferSchema) { //save the node in DOM until the end;
                            XmlNode node = xdoc.ReadNode(reader);
                            topNode.AppendChild(node);
                        }
                        else {
                            if (Columns.Count == 0) {
                                throw ExceptionBuilder.DataTableInferenceNotSupported();
                            }
                            if (xmlload == null)
                                xmlload = new XmlDataLoader(this, fIsXdr, topNode,  mode == XmlReadMode.IgnoreSchema);
                            xmlload.LoadData(reader);
                        }
                    } //end of the while

                    // read the closing tag of the current element
                    ReadEndElement(reader);

                    // now top node contains the data part
                    xdoc.AppendChild(topNode);

                    if (xmlload == null)
                        xmlload = new XmlDataLoader(this, fIsXdr, mode == XmlReadMode.IgnoreSchema);

                    if (mode == XmlReadMode.DiffGram) {
                        // we already got the diffs through XmlReader interface
                        RestoreConstraint(originalEnforceConstraint);
                        return ret;
                    }
    //todo
                    // Load Data
                    if (mode == XmlReadMode.InferSchema) {
                        if (Columns.Count == 0) {
                            throw ExceptionBuilder.DataTableInferenceNotSupported();
                        }

    // [....]                    xmlload.InferSchema(xdoc, null);
    // [....]                    xmlload.LoadData(xdoc);
                    }
                }
                RestoreConstraint(originalEnforceConstraint);

                return ret;
            }
            finally {
                // Dev11 904428: prepare and cleanup rowDiffId hashtable
                rowDiffIdUsage.Cleanup();
            }
        }
 internal XmlReadMode ReadXml(XmlReader reader, bool denyResolving)
 {
     XmlReadMode mode2;
     IntPtr ptr;
     Bid.ScopeEnter(out ptr, "<ds.DataSet.ReadXml|INFO> %d#, denyResolving=%d{bool}\n", this.ObjectID, denyResolving);
     try
     {
         try
         {
             bool flag7 = false;
             bool flag = false;
             bool flag6 = false;
             bool isXdr = false;
             int depth = -1;
             XmlReadMode auto = XmlReadMode.Auto;
             bool flag2 = false;
             bool flag5 = false;
             for (int i = 0; i < this.Tables.Count; i++)
             {
                 this.Tables[i].rowDiffId = null;
             }
             if (reader != null)
             {
                 if (this.Tables.Count == 0)
                 {
                     flag2 = true;
                 }
                 if (reader is XmlTextReader)
                 {
                     ((XmlTextReader) reader).WhitespaceHandling = WhitespaceHandling.Significant;
                 }
                 XmlDocument xdoc = new XmlDocument();
                 XmlDataLoader loader = null;
                 reader.MoveToContent();
                 if (reader.NodeType == XmlNodeType.Element)
                 {
                     depth = reader.Depth;
                 }
                 if (reader.NodeType != XmlNodeType.Element)
                 {
                     return auto;
                 }
                 if ((reader.LocalName == "diffgram") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                 {
                     this.ReadXmlDiffgram(reader);
                     this.ReadEndElement(reader);
                     return XmlReadMode.DiffGram;
                 }
                 if ((reader.LocalName == "Schema") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-data"))
                 {
                     this.ReadXDRSchema(reader);
                     return XmlReadMode.ReadSchema;
                 }
                 if ((reader.LocalName == "schema") && (reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema"))
                 {
                     this.ReadXSDSchema(reader, denyResolving);
                     return XmlReadMode.ReadSchema;
                 }
                 if ((reader.LocalName == "schema") && reader.NamespaceURI.StartsWith("http://www.w3.org/", StringComparison.Ordinal))
                 {
                     throw ExceptionBuilder.DataSetUnsupportedSchema("http://www.w3.org/2001/XMLSchema");
                 }
                 XmlElement topNode = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                 if (reader.HasAttributes)
                 {
                     int attributeCount = reader.AttributeCount;
                     for (int j = 0; j < attributeCount; j++)
                     {
                         reader.MoveToAttribute(j);
                         if (reader.NamespaceURI.Equals("http://www.w3.org/2000/xmlns/"))
                         {
                             topNode.SetAttribute(reader.Name, reader.GetAttribute(j));
                         }
                         else
                         {
                             XmlAttribute attribute = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                             attribute.Prefix = reader.Prefix;
                             attribute.Value = reader.GetAttribute(j);
                         }
                     }
                 }
                 reader.Read();
                 string str = reader.Value;
                 while (this.MoveToElement(reader, depth))
                 {
                     if ((reader.LocalName == "diffgram") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                     {
                         this.ReadXmlDiffgram(reader);
                         auto = XmlReadMode.DiffGram;
                     }
                     if ((!flag && !flag7) && ((reader.LocalName == "Schema") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-data")))
                     {
                         this.ReadXDRSchema(reader);
                         flag = true;
                         isXdr = true;
                     }
                     else if ((reader.LocalName == "schema") && (reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema"))
                     {
                         this.ReadXSDSchema(reader, denyResolving);
                         flag = true;
                     }
                     else
                     {
                         if ((reader.LocalName == "schema") && reader.NamespaceURI.StartsWith("http://www.w3.org/", StringComparison.Ordinal))
                         {
                             throw ExceptionBuilder.DataSetUnsupportedSchema("http://www.w3.org/2001/XMLSchema");
                         }
                         if (!(reader.LocalName == "diffgram") || !(reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                         {
                             goto Label_0340;
                         }
                         this.ReadXmlDiffgram(reader);
                         flag6 = true;
                         auto = XmlReadMode.DiffGram;
                     }
                     continue;
                 Label_0339:
                     reader.Read();
                 Label_0340:
                     if (!reader.EOF && (reader.NodeType == XmlNodeType.Whitespace))
                     {
                         goto Label_0339;
                     }
                     if (reader.NodeType == XmlNodeType.Element)
                     {
                         flag7 = true;
                         if (!flag && (this.Tables.Count == 0))
                         {
                             XmlNode newChild = xdoc.ReadNode(reader);
                             topNode.AppendChild(newChild);
                         }
                         else
                         {
                             if (loader == null)
                             {
                                 loader = new XmlDataLoader(this, isXdr, topNode, false);
                             }
                             loader.LoadData(reader);
                             flag5 = true;
                             if (flag)
                             {
                                 auto = XmlReadMode.ReadSchema;
                                 continue;
                             }
                             auto = XmlReadMode.IgnoreSchema;
                         }
                     }
                 }
                 this.ReadEndElement(reader);
                 bool flag4 = false;
                 bool fTopLevelTable = this.fTopLevelTable;
                 if ((!flag && (this.Tables.Count == 0)) && !topNode.HasChildNodes)
                 {
                     this.fTopLevelTable = true;
                     flag4 = true;
                     if ((str != null) && (str.Length > 0))
                     {
                         topNode.InnerText = str;
                     }
                 }
                 if ((!flag2 && (str != null)) && (str.Length > 0))
                 {
                     topNode.InnerText = str;
                 }
                 xdoc.AppendChild(topNode);
                 if (loader == null)
                 {
                     loader = new XmlDataLoader(this, isXdr, topNode, false);
                 }
                 if (!flag2 && !flag5)
                 {
                     XmlElement documentElement = xdoc.DocumentElement;
                     if ((documentElement.ChildNodes.Count == 0) || ((documentElement.ChildNodes.Count == 1) && (documentElement.FirstChild.GetType() == typeof(XmlText))))
                     {
                         bool flag8 = this.fTopLevelTable;
                         if (((this.DataSetName != documentElement.Name) && (this.namespaceURI != documentElement.NamespaceURI)) && this.Tables.Contains(documentElement.Name, (documentElement.NamespaceURI.Length == 0) ? null : documentElement.NamespaceURI, false, true))
                         {
                             this.fTopLevelTable = true;
                         }
                         try
                         {
                             loader.LoadData(xdoc);
                         }
                         finally
                         {
                             this.fTopLevelTable = flag8;
                         }
                     }
                 }
                 if (!flag6)
                 {
                     if (!flag && (this.Tables.Count == 0))
                     {
                         this.InferSchema(xdoc, null, XmlReadMode.Auto);
                         auto = XmlReadMode.InferSchema;
                         loader.FromInference = true;
                         try
                         {
                             loader.LoadData(xdoc);
                         }
                         finally
                         {
                             loader.FromInference = false;
                         }
                     }
                     if (flag4)
                     {
                         this.fTopLevelTable = fTopLevelTable;
                     }
                 }
             }
             return auto;
         }
         finally
         {
             for (int k = 0; k < this.Tables.Count; k++)
             {
                 this.Tables[k].rowDiffId = null;
             }
         }
     }
     finally
     {
         Bid.ScopeLeave(ref ptr);
     }
     return mode2;
 }
 internal XmlReadMode ReadXml(XmlReader reader, XmlReadMode mode, bool denyResolving)
 {
     XmlReadMode mode3;
     IntPtr ptr;
     Bid.ScopeEnter(out ptr, "<ds.DataSet.ReadXml|INFO> %d#, mode=%d{ds.XmlReadMode}, denyResolving=%d{bool}\n", this.ObjectID, (int) mode, denyResolving);
     try
     {
         bool flag2 = false;
         bool flag3 = false;
         bool isXdr = false;
         int depth = -1;
         XmlReadMode diffGram = mode;
         if (reader == null)
         {
             return diffGram;
         }
         if (mode == XmlReadMode.Auto)
         {
             return this.ReadXml(reader);
         }
         if (reader is XmlTextReader)
         {
             ((XmlTextReader) reader).WhitespaceHandling = WhitespaceHandling.Significant;
         }
         XmlDocument xdoc = new XmlDocument();
         if ((mode != XmlReadMode.Fragment) && (reader.NodeType == XmlNodeType.Element))
         {
             depth = reader.Depth;
         }
         reader.MoveToContent();
         XmlDataLoader loader = null;
         if (reader.NodeType == XmlNodeType.Element)
         {
             XmlElement topNode = null;
             if (mode == XmlReadMode.Fragment)
             {
                 xdoc.AppendChild(xdoc.CreateElement("ds_sqlXmlWraPPeR"));
                 topNode = xdoc.DocumentElement;
             }
             else
             {
                 if ((reader.LocalName == "diffgram") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                 {
                     if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema))
                     {
                         this.ReadXmlDiffgram(reader);
                         this.ReadEndElement(reader);
                         return diffGram;
                     }
                     reader.Skip();
                     return diffGram;
                 }
                 if ((reader.LocalName == "Schema") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-data"))
                 {
                     if (((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema)) && (mode != XmlReadMode.InferTypedSchema))
                     {
                         this.ReadXDRSchema(reader);
                         return diffGram;
                     }
                     reader.Skip();
                     return diffGram;
                 }
                 if ((reader.LocalName == "schema") && (reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema"))
                 {
                     if (((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema)) && (mode != XmlReadMode.InferTypedSchema))
                     {
                         this.ReadXSDSchema(reader, denyResolving);
                         return diffGram;
                     }
                     reader.Skip();
                     return diffGram;
                 }
                 if ((reader.LocalName == "schema") && reader.NamespaceURI.StartsWith("http://www.w3.org/", StringComparison.Ordinal))
                 {
                     throw ExceptionBuilder.DataSetUnsupportedSchema("http://www.w3.org/2001/XMLSchema");
                 }
                 topNode = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                 if (reader.HasAttributes)
                 {
                     int attributeCount = reader.AttributeCount;
                     for (int i = 0; i < attributeCount; i++)
                     {
                         reader.MoveToAttribute(i);
                         if (reader.NamespaceURI.Equals("http://www.w3.org/2000/xmlns/"))
                         {
                             topNode.SetAttribute(reader.Name, reader.GetAttribute(i));
                         }
                         else
                         {
                             XmlAttribute attribute = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                             attribute.Prefix = reader.Prefix;
                             attribute.Value = reader.GetAttribute(i);
                         }
                     }
                 }
                 reader.Read();
             }
             while (this.MoveToElement(reader, depth))
             {
                 if ((reader.LocalName == "Schema") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-data"))
                 {
                     if (((!flag2 && !flag3) && ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))) && (mode != XmlReadMode.InferTypedSchema))
                     {
                         this.ReadXDRSchema(reader);
                         flag2 = true;
                         isXdr = true;
                     }
                     else
                     {
                         reader.Skip();
                     }
                 }
                 else
                 {
                     if ((reader.LocalName == "schema") && (reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema"))
                     {
                         if (((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema)) && (mode != XmlReadMode.InferTypedSchema))
                         {
                             this.ReadXSDSchema(reader, denyResolving);
                             flag2 = true;
                         }
                         else
                         {
                             reader.Skip();
                         }
                         continue;
                     }
                     if ((reader.LocalName == "diffgram") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                     {
                         if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema))
                         {
                             this.ReadXmlDiffgram(reader);
                             diffGram = XmlReadMode.DiffGram;
                         }
                         else
                         {
                             reader.Skip();
                         }
                         continue;
                     }
                     if ((reader.LocalName == "schema") && reader.NamespaceURI.StartsWith("http://www.w3.org/", StringComparison.Ordinal))
                     {
                         throw ExceptionBuilder.DataSetUnsupportedSchema("http://www.w3.org/2001/XMLSchema");
                     }
                     if (mode == XmlReadMode.DiffGram)
                     {
                         reader.Skip();
                     }
                     else
                     {
                         flag3 = true;
                         if ((mode == XmlReadMode.InferSchema) || (mode == XmlReadMode.InferTypedSchema))
                         {
                             XmlNode newChild = xdoc.ReadNode(reader);
                             topNode.AppendChild(newChild);
                             continue;
                         }
                         if (loader == null)
                         {
                             loader = new XmlDataLoader(this, isXdr, topNode, mode == XmlReadMode.IgnoreSchema);
                         }
                         loader.LoadData(reader);
                     }
                 }
             }
             this.ReadEndElement(reader);
             xdoc.AppendChild(topNode);
             if (loader == null)
             {
                 loader = new XmlDataLoader(this, isXdr, mode == XmlReadMode.IgnoreSchema);
             }
             if (mode == XmlReadMode.DiffGram)
             {
                 return diffGram;
             }
             if ((mode == XmlReadMode.InferSchema) || (mode == XmlReadMode.InferTypedSchema))
             {
                 this.InferSchema(xdoc, null, mode);
                 diffGram = XmlReadMode.InferSchema;
                 loader.FromInference = true;
                 try
                 {
                     loader.LoadData(xdoc);
                 }
                 finally
                 {
                     loader.FromInference = false;
                 }
             }
         }
         mode3 = diffGram;
     }
     finally
     {
         Bid.ScopeLeave(ref ptr);
     }
     return mode3;
 }
        internal XmlReadMode ReadXml(XmlReader reader, XmlReadMode mode, bool denyResolving)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataSet.ReadXml|INFO> %d#, mode=%d{ds.XmlReadMode}, denyResolving=%d{bool}\n", ObjectID, (int)mode, denyResolving);
            try {

                XmlReadMode ret = mode;

                if (reader == null)
                    return ret;

                if (mode == XmlReadMode.Auto) {
                    // Dev11 915079: nested ReadXml calls on the same DataSet must be done outside of RowDiffIdUsage scope
                    return ReadXml(reader);
                }

                DataTable.DSRowDiffIdUsageSection rowDiffIdUsage = new DataTable.DSRowDiffIdUsageSection();
                try {

                    bool fSchemaFound = false;
                    bool fDataFound = false;
                    bool fIsXdr = false;
                    int iCurrentDepth = -1;

                    // Dev11 904428: prepare and cleanup rowDiffId hashtable
                    rowDiffIdUsage.Prepare(this);

                    if (reader is XmlTextReader)
                        ((XmlTextReader)reader).WhitespaceHandling = WhitespaceHandling.Significant;

                    XmlDocument xdoc = new XmlDocument(); // we may need this to infer the schema

                    if ((mode != XmlReadMode.Fragment) && (reader.NodeType == XmlNodeType.Element))
                        iCurrentDepth = reader.Depth;

                    reader.MoveToContent();
                    XmlDataLoader xmlload = null;

                    if (reader.NodeType == XmlNodeType.Element) {
                        XmlElement topNode = null;
                        if (mode == XmlReadMode.Fragment) {
                            xdoc.AppendChild(xdoc.CreateElement("ds_sqlXmlWraPPeR"));
                            topNode = xdoc.DocumentElement;
                        }
                        else { //handle the top node
                            if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                                if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema)) {
                                    this.ReadXmlDiffgram(reader);
                                    // read the closing tag of the current element
                                    ReadEndElement(reader);
                                }
                                else {
                                    reader.Skip();
                                }
                                return ret;
                            }

                            if (reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI == Keywords.XDRNS) {
                                // load XDR schema and exit
                                if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema) &&
                                    (mode != XmlReadMode.InferTypedSchema))
                                {
                                    ReadXDRSchema(reader);
                                }
                                else {
                                    reader.Skip();
                                }
                                return ret; //since the top level element is a schema return
                            }

                            if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI == Keywords.XSDNS) {
                                // load XSD schema and exit
                                if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema) &&
                                    (mode != XmlReadMode.InferTypedSchema))
                                {
                                    ReadXSDSchema(reader, denyResolving);
                                }
                                else
                                    reader.Skip();
                                return ret; //since the top level element is a schema return
                            }

                            if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))
                                throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);

                            // now either the top level node is a table and we load it through dataReader...
                            // ... or backup the top node and all its attributes
                            topNode = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                            if (reader.HasAttributes) {
                                int attrCount = reader.AttributeCount;
                                for (int i = 0; i < attrCount; i++) {
                                    reader.MoveToAttribute(i);
                                    if (reader.NamespaceURI.Equals(Keywords.XSD_XMLNS_NS))
                                        topNode.SetAttribute(reader.Name, reader.GetAttribute(i));
                                    else {
                                        XmlAttribute attr = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                                        attr.Prefix = reader.Prefix;
                                        attr.Value = reader.GetAttribute(i);
                                    }
                                }
                            }
                            reader.Read();
                        }

                        while (MoveToElement(reader, iCurrentDepth)) {

                            if (reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI == Keywords.XDRNS) {
                                // load XDR schema
                                if (!fSchemaFound && !fDataFound && (mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema) &&
                                    (mode != XmlReadMode.InferTypedSchema))
                                {
                                    ReadXDRSchema(reader);
                                    fSchemaFound = true;
                                    fIsXdr = true;
                                }
                                else {
                                    reader.Skip();
                                }
                                continue;
                            }

                            if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI == Keywords.XSDNS) {
                                // load XSD schema and exit
                                if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema) &&
                                    (mode != XmlReadMode.InferTypedSchema))
                                {
                                    ReadXSDSchema(reader, denyResolving);
                                    fSchemaFound = true;
                                }
                                else {
                                    reader.Skip();
                                }
                                continue;
                            }

                            if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                                if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema)) {
                                    this.ReadXmlDiffgram(reader);
                                    ret = XmlReadMode.DiffGram;
                                }
                                else {
                                    reader.Skip();
                                }
                                continue;
                            }

                            if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))
                                throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);

                            if (mode == XmlReadMode.DiffGram) {
                                reader.Skip();
                                continue; // we do not read data in diffgram mode
                            }

                            // if we are here we found some data
                            fDataFound = true;

                            if (mode == XmlReadMode.InferSchema || mode == XmlReadMode.InferTypedSchema) { //save the node in DOM until the end;
                                XmlNode node = xdoc.ReadNode(reader);
                                topNode.AppendChild(node);
                            }
                            else {
                                if (xmlload == null)
                                    xmlload = new XmlDataLoader(this, fIsXdr, topNode, mode == XmlReadMode.IgnoreSchema);
                                xmlload.LoadData(reader);
                            }
                        } //end of the while

                        // read the closing tag of the current element
                        ReadEndElement(reader);

                        // now top node contains the data part
                        xdoc.AppendChild(topNode);
                        if (xmlload == null)
                            xmlload = new XmlDataLoader(this, fIsXdr, mode == XmlReadMode.IgnoreSchema);

                        if (mode == XmlReadMode.DiffGram) {
                            // we already got the diffs through XmlReader interface
                            return ret;
                        }

                        // Load Data
                        if (mode == XmlReadMode.InferSchema || mode == XmlReadMode.InferTypedSchema) {
                            InferSchema(xdoc, null, mode);
                            ret = XmlReadMode.InferSchema;
                            xmlload.FromInference = true;
                            //                }
                            try {
                                xmlload.LoadData(xdoc);
                            }
                            finally {
                                xmlload.FromInference = false;
                            }
                        }
                    }

                    return ret;
                }
                finally {
                    // Dev11 904428: prepare and cleanup rowDiffId hashtable
                    rowDiffIdUsage.Cleanup();
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }
        private void ReadXmlDiffgram(XmlReader reader) {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataSet.ReadXmlDiffgram|INFO> %d#\n", ObjectID);
            try {
                int d = reader.Depth;
                bool fEnforce = this.EnforceConstraints;
                this.EnforceConstraints = false;
                DataSet newDs;
                bool isEmpty = this.IsEmpty();

                if (isEmpty) {
                    newDs = this;
                }
                else {
                    newDs = this.Clone();
                    newDs.EnforceConstraints = false;
                }

                foreach (DataTable t in newDs.Tables) {
                    t.Rows.nullInList = 0;
                }
                reader.MoveToContent();
                if ((reader.LocalName != Keywords.DIFFGRAM) && (reader.NamespaceURI != Keywords.DFFNS))
                    return;
                reader.Read();
                if (reader.NodeType == XmlNodeType.Whitespace)
                    MoveToElement(reader, reader.Depth - 1 /*iCurrentDepth*/); // skip over whitespaces.

                newDs.fInLoadDiffgram = true;

                if (reader.Depth > d) {
                    if ((reader.NamespaceURI != Keywords.DFFNS) && (reader.NamespaceURI != Keywords.MSDNS)) {
                        //we should be inside the dataset part
                        XmlDocument xdoc = new XmlDocument();
                        XmlElement node = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Whitespace) {
                            MoveToElement(reader, reader.Depth - 1 /*iCurrentDepth*/); // skip over whitespaces.
                        }
                        if (reader.Depth - 1 > d) {
                            XmlDataLoader xmlload = new XmlDataLoader(newDs, false, node, false);
                            xmlload.isDiffgram = true; // turn on the special processing
                            xmlload.LoadData(reader);
                        }
                        ReadEndElement(reader);
                        if (reader.NodeType == XmlNodeType.Whitespace) {
                            MoveToElement(reader, reader.Depth - 1 /*iCurrentDepth*/); // skip over whitespaces.
                        }
                    }
                    Debug.Assert(reader.NodeType != XmlNodeType.Whitespace, "Should not be on Whitespace node");

                    if (((reader.LocalName == Keywords.SQL_BEFORE) && (reader.NamespaceURI == Keywords.DFFNS)) ||
                        ((reader.LocalName == Keywords.MSD_ERRORS) && (reader.NamespaceURI == Keywords.DFFNS)))

                    {
                        //this will consume the changes and the errors part
                        XMLDiffLoader diffLoader = new XMLDiffLoader();
                        diffLoader.LoadDiffGram(newDs, reader);
                    }

                    // get to the closing diff tag
                    while (reader.Depth > d) {
                        reader.Read();
                    }
                    // read the closing tag
                    ReadEndElement(reader);
                }

                foreach (DataTable t in newDs.Tables) {
                    if (t.Rows.nullInList > 0)
                        throw ExceptionBuilder.RowInsertMissing(t.TableName);
                }

                newDs.fInLoadDiffgram = false;

                //terrible performance!
                foreach (DataTable t in newDs.Tables) {
                    DataRelation[] nestedParentRelations = t.NestedParentRelations;
                    foreach (DataRelation rel in nestedParentRelations) {
                        if (rel.ParentTable == t) {
                            foreach (DataRow r in t.Rows) {
                                foreach (DataRelation rel2 in nestedParentRelations) {
                                    r.CheckForLoops(rel2);
                                }
                            }
                        }
                    }
                }

                if (!isEmpty) {
                    this.Merge(newDs);
                    if (this.dataSetName == "NewDataSet")
                        this.dataSetName = newDs.dataSetName;
                    newDs.EnforceConstraints = fEnforce;
                }
                this.EnforceConstraints = fEnforce;
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }

        }
        internal XmlReadMode ReadXml(XmlReader reader, bool denyResolving)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataSet.ReadXml|INFO> %d#, denyResolving=%d{bool}\n", ObjectID, denyResolving);
            try {
                
                DataTable.DSRowDiffIdUsageSection rowDiffIdUsage = new DataTable.DSRowDiffIdUsageSection();
                try {
                    bool fDataFound = false;
                    bool fSchemaFound = false;
                    bool fDiffsFound = false;
                    bool fIsXdr = false;
                    int iCurrentDepth = -1;
                    XmlReadMode ret = XmlReadMode.Auto;
                    bool isEmptyDataSet = false;
                    bool topNodeIsProcessed = false; // we chanche topnode and there is just one case that we miss to process it
                    // it is : <elem attrib1="Attrib">txt</elem>

                    // clear the hashtable to avoid conflicts between diffgrams, SqlHotFix 782
                    rowDiffIdUsage.Prepare(this);

                    if (reader == null)
                        return ret;

                    if (Tables.Count == 0) {
                        isEmptyDataSet = true;
                    }

                    if (reader is XmlTextReader)
                        ((XmlTextReader)reader).WhitespaceHandling = WhitespaceHandling.Significant;

                    XmlDocument xdoc = new XmlDocument(); // we may need this to infer the schema
                    XmlDataLoader xmlload = null;


                    reader.MoveToContent();

                    if (reader.NodeType == XmlNodeType.Element)
                        iCurrentDepth = reader.Depth;

                    if (reader.NodeType == XmlNodeType.Element) {
                        if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                            this.ReadXmlDiffgram(reader);
                            // read the closing tag of the current element
                            ReadEndElement(reader);
                            return XmlReadMode.DiffGram;
                        }

                        // if reader points to the schema load it
                        if (reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI == Keywords.XDRNS) {
                            // load XDR schema and exit
                            ReadXDRSchema(reader);
                            return XmlReadMode.ReadSchema; //since the top level element is a schema return
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI == Keywords.XSDNS) {
                            // load XSD schema and exit
                            ReadXSDSchema(reader, denyResolving);
                            return XmlReadMode.ReadSchema; //since the top level element is a schema return
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))
                            throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);

                        // now either the top level node is a table and we load it through dataReader...

                        // ... or backup the top node and all its attributes because we may need to InferSchema
                        XmlElement topNode = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                        if (reader.HasAttributes) {
                            int attrCount = reader.AttributeCount;
                            for (int i = 0; i < attrCount; i++) {
                                reader.MoveToAttribute(i);
                                if (reader.NamespaceURI.Equals(Keywords.XSD_XMLNS_NS))
                                    topNode.SetAttribute(reader.Name, reader.GetAttribute(i));
                                else {
                                    XmlAttribute attr = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                                    attr.Prefix = reader.Prefix;
                                    attr.Value = reader.GetAttribute(i);
                                }
                            }
                        }
                        reader.Read();
                        string rootNodeSimpleContent = reader.Value;

                        while (MoveToElement(reader, iCurrentDepth)) {

                            if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                                this.ReadXmlDiffgram(reader);
                                // read the closing tag of the current element
                                // YUKON FIX                            ReadEndElement(reader);
                                //                            return XmlReadMode.DiffGram;
                                ret = XmlReadMode.DiffGram; // continue reading for multiple schemas
                            }

                            // if reader points to the schema load it...


                            if (!fSchemaFound && !fDataFound && reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI == Keywords.XDRNS) {
                                // load XDR schema and exit
                                ReadXDRSchema(reader);
                                fSchemaFound = true;
                                fIsXdr = true;
                                continue;
                            }

                            if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI == Keywords.XSDNS) {
                                // load XSD schema and exit
                                ReadXSDSchema(reader, denyResolving);
                                fSchemaFound = true;
                                continue;
                            }

                            if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))
                                throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);

                            if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                                this.ReadXmlDiffgram(reader);
                                fDiffsFound = true;
                                ret = XmlReadMode.DiffGram;
                            }
                            else {
                                // We have found data IFF the reader.NodeType == Element and reader.depth == currentDepth-1
                                // if reader.NodeType == whitespace, skip all white spaces.
                                // skip processing i.e. continue if the first non-whitespace node is not of type element.
                                while (!reader.EOF && reader.NodeType == XmlNodeType.Whitespace)
                                    reader.Read();
                                if (reader.NodeType != XmlNodeType.Element)
                                    continue;
                                // we found data here
                                fDataFound = true;

                                if (!fSchemaFound && Tables.Count == 0) {
                                    XmlNode node = xdoc.ReadNode(reader);
                                    topNode.AppendChild(node);
                                }
                                else {
                                    if (xmlload == null)
                                        xmlload = new XmlDataLoader(this, fIsXdr, topNode, false);
                                    xmlload.LoadData(reader);
                                    topNodeIsProcessed = true; // we process the topnode
                                    if (fSchemaFound)
                                        ret = XmlReadMode.ReadSchema;
                                    else
                                        ret = XmlReadMode.IgnoreSchema;
                                }
                            }

                        }
                        // read the closing tag of the current element
                        ReadEndElement(reader);
                        bool isfTopLevelTableSet = false;
                        bool tmpValue = this.fTopLevelTable;
                        //While inference we ignore root elements text content
                        if (!fSchemaFound && Tables.Count == 0 && !topNode.HasChildNodes) { //We shoule not come add SC of root elemnt to topNode if we are not infering
                            this.fTopLevelTable = true;
                            isfTopLevelTableSet = true;
                            if ((rootNodeSimpleContent != null && rootNodeSimpleContent.Length > 0))
                                topNode.InnerText = rootNodeSimpleContent;
                        }
                        if (!isEmptyDataSet) {
                            if ((rootNodeSimpleContent != null && rootNodeSimpleContent.Length > 0))
                                topNode.InnerText = rootNodeSimpleContent;
                        }

                        // now top node contains the data part
                        xdoc.AppendChild(topNode);

                        if (xmlload == null)
                            xmlload = new XmlDataLoader(this, fIsXdr, topNode, false);

                        if (!isEmptyDataSet && !topNodeIsProcessed) {
                            XmlElement root = xdoc.DocumentElement;
                            Debug.Assert(root.NamespaceURI != null, "root.NamespaceURI should not ne null, it should be empty string");
                            // just recognize that below given Xml represents datatable in toplevel
                            //<table attr1="foo" attr2="bar" table_Text="junk">text</table>
                            // only allow root element with simple content, if any
                            if (root.ChildNodes.Count == 0 ||
                                ((root.ChildNodes.Count == 1) && root.FirstChild.GetType() == typeof(System.Xml.XmlText))) {
                                bool initfTopLevelTable = this.fTopLevelTable;
                                // if root element maps to a datatable
                                // ds and dt cant have the samm name and ns at the same time, how to write to xml
                                if (this.DataSetName != root.Name && this.namespaceURI != root.NamespaceURI &&
                                    Tables.Contains(root.Name, (root.NamespaceURI.Length == 0) ? null : root.NamespaceURI, false, true)) {
                                    this.fTopLevelTable = true;
                                }
                                try {
                                    xmlload.LoadData(xdoc);
                                }
                                finally {
                                    this.fTopLevelTable = initfTopLevelTable; // this is not for inference, we have schema and we were skipping
                                    // topnode where it was a datatable, We must restore the value
                                }
                            }
                        }// above check and below check are orthogonal
                        // so we InferSchema
                        if (!fDiffsFound) {
                            // Load Data
                            if (!fSchemaFound && Tables.Count == 0) {
                                InferSchema(xdoc, null, XmlReadMode.Auto);
                                ret = XmlReadMode.InferSchema;
                                xmlload.FromInference = true;
                                try {
                                    xmlload.LoadData(xdoc);
                                }
                                finally {
                                    xmlload.FromInference = false;
                                }
                            }
                            //We dont need this assignement. Once we set it(where we set it during inference), it won't be changed
                            if (isfTopLevelTableSet)
                                this.fTopLevelTable = tmpValue;
                        }
                    }

                    return ret;
                }
                finally {
                    rowDiffIdUsage.Cleanup();
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }
 internal XmlReadMode ReadXml(XmlReader reader, XmlReadMode mode, bool denyResolving)
 {
     bool flag3 = false;
     bool flag4 = false;
     bool isXdr = false;
     int depth = -1;
     XmlReadMode diffGram = mode;
     if (reader != null)
     {
         bool originalEnforceConstraint = false;
         if (this.DataSet != null)
         {
             originalEnforceConstraint = this.DataSet.EnforceConstraints;
             this.DataSet.EnforceConstraints = false;
         }
         else
         {
             originalEnforceConstraint = this.EnforceConstraints;
             this.EnforceConstraints = false;
         }
         if (reader is XmlTextReader)
         {
             ((XmlTextReader) reader).WhitespaceHandling = WhitespaceHandling.Significant;
         }
         XmlDocument document = new XmlDocument();
         if ((mode != XmlReadMode.Fragment) && (reader.NodeType == XmlNodeType.Element))
         {
             depth = reader.Depth;
         }
         reader.MoveToContent();
         if ((this.Columns.Count == 0) && this.IsEmptyXml(reader))
         {
             reader.Read();
             return diffGram;
         }
         XmlDataLoader loader = null;
         if (reader.NodeType == XmlNodeType.Element)
         {
             XmlElement topNode = null;
             if (mode == XmlReadMode.Fragment)
             {
                 document.AppendChild(document.CreateElement("ds_sqlXmlWraPPeR"));
                 topNode = document.DocumentElement;
             }
             else
             {
                 if ((reader.LocalName == "diffgram") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                 {
                     if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema))
                     {
                         if (this.Columns.Count == 0)
                         {
                             if (!reader.IsEmptyElement)
                             {
                                 throw ExceptionBuilder.DataTableInferenceNotSupported();
                             }
                             reader.Read();
                             return XmlReadMode.DiffGram;
                         }
                         this.ReadXmlDiffgram(reader);
                         this.ReadEndElement(reader);
                     }
                     else
                     {
                         reader.Skip();
                     }
                     this.RestoreConstraint(originalEnforceConstraint);
                     return diffGram;
                 }
                 if ((reader.LocalName == "Schema") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-data"))
                 {
                     if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))
                     {
                         this.ReadXDRSchema(reader);
                     }
                     else
                     {
                         reader.Skip();
                     }
                     this.RestoreConstraint(originalEnforceConstraint);
                     return diffGram;
                 }
                 if ((reader.LocalName == "schema") && (reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema"))
                 {
                     if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))
                     {
                         this.ReadXmlSchema(reader, denyResolving);
                     }
                     else
                     {
                         reader.Skip();
                     }
                     this.RestoreConstraint(originalEnforceConstraint);
                     return diffGram;
                 }
                 if ((reader.LocalName == "schema") && reader.NamespaceURI.StartsWith("http://www.w3.org/", StringComparison.Ordinal))
                 {
                     if (this.DataSet != null)
                     {
                         this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                     }
                     else
                     {
                         this.enforceConstraints = originalEnforceConstraint;
                     }
                     throw ExceptionBuilder.DataSetUnsupportedSchema("http://www.w3.org/2001/XMLSchema");
                 }
                 topNode = document.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                 if (reader.HasAttributes)
                 {
                     int attributeCount = reader.AttributeCount;
                     for (int i = 0; i < attributeCount; i++)
                     {
                         reader.MoveToAttribute(i);
                         if (reader.NamespaceURI.Equals("http://www.w3.org/2000/xmlns/"))
                         {
                             topNode.SetAttribute(reader.Name, reader.GetAttribute(i));
                         }
                         else
                         {
                             XmlAttribute attribute = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                             attribute.Prefix = reader.Prefix;
                             attribute.Value = reader.GetAttribute(i);
                         }
                     }
                 }
                 reader.Read();
             }
             while (this.MoveToElement(reader, depth))
             {
                 if ((reader.LocalName == "Schema") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-data"))
                 {
                     if ((!flag3 && !flag4) && ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema)))
                     {
                         this.ReadXDRSchema(reader);
                         flag3 = true;
                         isXdr = true;
                     }
                     else
                     {
                         reader.Skip();
                     }
                 }
                 else
                 {
                     if ((reader.LocalName == "schema") && (reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema"))
                     {
                         if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))
                         {
                             this.ReadXmlSchema(reader, denyResolving);
                             flag3 = true;
                         }
                         else
                         {
                             reader.Skip();
                         }
                         continue;
                     }
                     if ((reader.LocalName == "diffgram") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                     {
                         if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema))
                         {
                             if (this.Columns.Count == 0)
                             {
                                 if (!reader.IsEmptyElement)
                                 {
                                     throw ExceptionBuilder.DataTableInferenceNotSupported();
                                 }
                                 reader.Read();
                                 return XmlReadMode.DiffGram;
                             }
                             this.ReadXmlDiffgram(reader);
                             diffGram = XmlReadMode.DiffGram;
                         }
                         else
                         {
                             reader.Skip();
                         }
                         continue;
                     }
                     if ((reader.LocalName == "schema") && reader.NamespaceURI.StartsWith("http://www.w3.org/", StringComparison.Ordinal))
                     {
                         if (this.DataSet != null)
                         {
                             this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                         }
                         else
                         {
                             this.enforceConstraints = originalEnforceConstraint;
                         }
                         throw ExceptionBuilder.DataSetUnsupportedSchema("http://www.w3.org/2001/XMLSchema");
                     }
                     if (mode == XmlReadMode.DiffGram)
                     {
                         reader.Skip();
                     }
                     else
                     {
                         flag4 = true;
                         if (mode == XmlReadMode.InferSchema)
                         {
                             XmlNode newChild = document.ReadNode(reader);
                             topNode.AppendChild(newChild);
                             continue;
                         }
                         if (this.Columns.Count == 0)
                         {
                             throw ExceptionBuilder.DataTableInferenceNotSupported();
                         }
                         if (loader == null)
                         {
                             loader = new XmlDataLoader(this, isXdr, topNode, mode == XmlReadMode.IgnoreSchema);
                         }
                         loader.LoadData(reader);
                     }
                 }
             }
             this.ReadEndElement(reader);
             document.AppendChild(topNode);
             if (loader == null)
             {
                 loader = new XmlDataLoader(this, isXdr, mode == XmlReadMode.IgnoreSchema);
             }
             if (mode == XmlReadMode.DiffGram)
             {
                 this.RestoreConstraint(originalEnforceConstraint);
                 return diffGram;
             }
             if ((mode == XmlReadMode.InferSchema) && (this.Columns.Count == 0))
             {
                 throw ExceptionBuilder.DataTableInferenceNotSupported();
             }
         }
         this.RestoreConstraint(originalEnforceConstraint);
     }
     return diffGram;
 }
 internal XmlReadMode ReadXml(XmlReader reader, bool denyResolving)
 {
     XmlReadMode mode;
     IntPtr ptr;
     Bid.ScopeEnter(out ptr, "<ds.DataTable.ReadXml|INFO> %d#, denyResolving=%d{bool}\n", this.ObjectID, denyResolving);
     try
     {
         try
         {
             bool flag5 = false;
             bool flag2 = false;
             bool flag4 = false;
             bool isXdr = false;
             int depth = -1;
             XmlReadMode auto = XmlReadMode.Auto;
             this.rowDiffId = null;
             if (reader != null)
             {
                 bool originalEnforceConstraint = false;
                 if (this.DataSet != null)
                 {
                     originalEnforceConstraint = this.DataSet.EnforceConstraints;
                     this.DataSet.EnforceConstraints = false;
                 }
                 else
                 {
                     originalEnforceConstraint = this.EnforceConstraints;
                     this.EnforceConstraints = false;
                 }
                 if (reader is XmlTextReader)
                 {
                     ((XmlTextReader) reader).WhitespaceHandling = WhitespaceHandling.Significant;
                 }
                 XmlDocument document = new XmlDocument();
                 XmlDataLoader loader = null;
                 reader.MoveToContent();
                 if ((this.Columns.Count == 0) && this.IsEmptyXml(reader))
                 {
                     reader.Read();
                     return auto;
                 }
                 if (reader.NodeType == XmlNodeType.Element)
                 {
                     depth = reader.Depth;
                     if ((reader.LocalName == "diffgram") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                     {
                         if (this.Columns.Count == 0)
                         {
                             if (!reader.IsEmptyElement)
                             {
                                 throw ExceptionBuilder.DataTableInferenceNotSupported();
                             }
                             reader.Read();
                             return XmlReadMode.DiffGram;
                         }
                         this.ReadXmlDiffgram(reader);
                         this.ReadEndElement(reader);
                         this.RestoreConstraint(originalEnforceConstraint);
                         return XmlReadMode.DiffGram;
                     }
                     if ((reader.LocalName == "Schema") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-data"))
                     {
                         this.ReadXDRSchema(reader);
                         this.RestoreConstraint(originalEnforceConstraint);
                         return XmlReadMode.ReadSchema;
                     }
                     if ((reader.LocalName == "schema") && (reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema"))
                     {
                         this.ReadXmlSchema(reader, denyResolving);
                         this.RestoreConstraint(originalEnforceConstraint);
                         return XmlReadMode.ReadSchema;
                     }
                     if ((reader.LocalName == "schema") && reader.NamespaceURI.StartsWith("http://www.w3.org/", StringComparison.Ordinal))
                     {
                         if (this.DataSet != null)
                         {
                             this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                         }
                         else
                         {
                             this.enforceConstraints = originalEnforceConstraint;
                         }
                         throw ExceptionBuilder.DataSetUnsupportedSchema("http://www.w3.org/2001/XMLSchema");
                     }
                     XmlElement topNode = document.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                     if (reader.HasAttributes)
                     {
                         int attributeCount = reader.AttributeCount;
                         for (int i = 0; i < attributeCount; i++)
                         {
                             reader.MoveToAttribute(i);
                             if (reader.NamespaceURI.Equals("http://www.w3.org/2000/xmlns/"))
                             {
                                 topNode.SetAttribute(reader.Name, reader.GetAttribute(i));
                             }
                             else
                             {
                                 XmlAttribute attribute = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                                 attribute.Prefix = reader.Prefix;
                                 attribute.Value = reader.GetAttribute(i);
                             }
                         }
                     }
                     reader.Read();
                     while (this.MoveToElement(reader, depth))
                     {
                         if ((reader.LocalName == "diffgram") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                         {
                             this.ReadXmlDiffgram(reader);
                             this.ReadEndElement(reader);
                             this.RestoreConstraint(originalEnforceConstraint);
                             return XmlReadMode.DiffGram;
                         }
                         if ((!flag2 && !flag5) && ((reader.LocalName == "Schema") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-data")))
                         {
                             this.ReadXDRSchema(reader);
                             flag2 = true;
                             isXdr = true;
                         }
                         else
                         {
                             if ((reader.LocalName == "schema") && (reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema"))
                             {
                                 this.ReadXmlSchema(reader, denyResolving);
                                 flag2 = true;
                                 continue;
                             }
                             if ((reader.LocalName == "schema") && reader.NamespaceURI.StartsWith("http://www.w3.org/", StringComparison.Ordinal))
                             {
                                 if (this.DataSet != null)
                                 {
                                     this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                                 }
                                 else
                                 {
                                     this.enforceConstraints = originalEnforceConstraint;
                                 }
                                 throw ExceptionBuilder.DataSetUnsupportedSchema("http://www.w3.org/2001/XMLSchema");
                             }
                             if ((reader.LocalName == "diffgram") && (reader.NamespaceURI == "urn:schemas-microsoft-com:xml-diffgram-v1"))
                             {
                                 this.ReadXmlDiffgram(reader);
                                 flag4 = true;
                                 auto = XmlReadMode.DiffGram;
                             }
                             else
                             {
                                 flag5 = true;
                                 if (!flag2 && (this.Columns.Count == 0))
                                 {
                                     XmlNode newChild = document.ReadNode(reader);
                                     topNode.AppendChild(newChild);
                                     continue;
                                 }
                                 if (loader == null)
                                 {
                                     loader = new XmlDataLoader(this, isXdr, topNode, false);
                                 }
                                 loader.LoadData(reader);
                                 if (flag2)
                                 {
                                     auto = XmlReadMode.ReadSchema;
                                     continue;
                                 }
                                 auto = XmlReadMode.IgnoreSchema;
                             }
                         }
                     }
                     this.ReadEndElement(reader);
                     document.AppendChild(topNode);
                     if (!flag2 && (this.Columns.Count == 0))
                     {
                         if (!this.IsEmptyXml(reader))
                         {
                             throw ExceptionBuilder.DataTableInferenceNotSupported();
                         }
                         reader.Read();
                         return auto;
                     }
                     if (loader == null)
                     {
                         loader = new XmlDataLoader(this, isXdr, false);
                     }
                 }
                 this.RestoreConstraint(originalEnforceConstraint);
             }
             return auto;
         }
         finally
         {
             this.rowDiffId = null;
         }
     }
     finally
     {
         Bid.ScopeLeave(ref ptr);
     }
     return mode;
 }