System.Data.Tests.DataTableReadWriteXmlTest.TestWriteXml C# (CSharp) Méthode

TestWriteXml() private méthode

private TestWriteXml ( ) : void
Résultat void
        public void TestWriteXml()
        {
            DataSet ds;
            DataTable dtMainInDS, dtChildInDS, dtMain;

            GenerateTestData(out ds,
                             out dtMainInDS,
                             out dtChildInDS,
                             out dtMain);

            StringWriter sw = new StringWriter();

            // Get XML for DataSet writes.
            sw.GetStringBuilder().Length = 0;
            ds.WriteXml(sw);
            string xmlDSNone = sw.ToString().Replace("\n", EOL).Replace("\r\r\n", EOL);

            sw.GetStringBuilder().Length = 0;
            ds.WriteXml(sw, XmlWriteMode.DiffGram);
            string xmlDSDiffGram = sw.ToString().Replace("\n", EOL).Replace("\r\r\n", EOL);

            sw.GetStringBuilder().Length = 0;
            ds.WriteXml(sw, XmlWriteMode.WriteSchema);
            string xmlDSWriteSchema = sw.ToString();

            // Get XML for recursive DataTable writes of the same data as in
            // the DataSet.
            sw.GetStringBuilder().Length = 0;
            dtMainInDS.WriteXml(sw, true);
            string xmlDTNone = sw.ToString();

            sw.GetStringBuilder().Length = 0;
            dtMainInDS.WriteXml(sw, XmlWriteMode.DiffGram, true);
            string xmlDTDiffGram = sw.ToString();

            sw.GetStringBuilder().Length = 0;
            dtMainInDS.WriteXml(sw, XmlWriteMode.WriteSchema, true);
            string xmlDTWriteSchema = sw.ToString();

            // The schema XML written by the DataTable call has an extra element
            // in the element for the dataset schema definition.  We remove that
            // extra attribute and then check to see if the rest of the xml is
            // identical.
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlDTWriteSchema);
            XmlNode node = doc.DocumentElement.FirstChild.FirstChild;
            XmlAttribute a = (XmlAttribute)node.Attributes.GetNamedItem("msdata:MainDataTable");
            Assert.NotNull(a);
            Assert.Equal("Main", a.Value);

            node.Attributes.Remove(a);
            sw.GetStringBuilder().Length = 0;
            doc.Save(sw);
            xmlDTWriteSchema = sw.ToString();

            StandardizeXmlFormat(ref xmlDSWriteSchema);

            Assert.Equal(xmlDSNone, xmlDTNone);
            Assert.Equal(xmlDSDiffGram, xmlDTDiffGram);
            Assert.True(xmlDSWriteSchema.IndexOf("UseCurrentLocale") > 0);
            Assert.Equal(xmlDSWriteSchema, xmlDTWriteSchema);

            // Now that we've tested writing tables (including children),
            // we will go on to test the cases where the hierarchy flag
            // is false.  For this, we will test one table inside the
            // dataset and one table outside the dataset.

            // First, we fix our test DataSet to only have a single table
            // with no relations.  Then, we go about comparing the XML.
            // Get XML for DataSet writes.
            ds.Tables[1].Constraints.Remove(ds.Tables[1].Constraints[0]);
            ds.Tables[0].Constraints.Remove(ds.Tables[0].Constraints[0]);
            ds.Tables[0].ChildRelations.Remove("MainToChild");
            ds.Tables.Remove("Child");

            sw.GetStringBuilder().Length = 0;
            ds.WriteXml(sw);
            xmlDSNone = sw.ToString().Replace("\n", EOL).Replace("\r\r\n", EOL);

            sw.GetStringBuilder().Length = 0;
            ds.WriteXml(sw, XmlWriteMode.DiffGram);
            xmlDSDiffGram = sw.ToString().Replace("\n", EOL).Replace("\r\r\n", EOL);

            sw.GetStringBuilder().Length = 0;
            ds.WriteXml(sw, XmlWriteMode.WriteSchema);
            xmlDSWriteSchema = sw.ToString();

            // Get all the DataTable.WriteXml results.
            sw.GetStringBuilder().Length = 0;
            dtMainInDS.WriteXml(sw);
            string xmlDTNoneInDS = sw.ToString();

            sw.GetStringBuilder().Length = 0;
            dtMainInDS.WriteXml(sw, XmlWriteMode.DiffGram);
            string xmlDTDiffGramInDS = sw.ToString();

            sw.GetStringBuilder().Length = 0;
            dtMainInDS.WriteXml(sw, XmlWriteMode.WriteSchema);
            string xmlDTWriteSchemaInDS = sw.ToString();

            sw.GetStringBuilder().Length = 0;
            dtMain.WriteXml(sw);
            string xmlDTNoneNoDS = sw.ToString();

            sw.GetStringBuilder().Length = 0;
            dtMain.WriteXml(sw, XmlWriteMode.DiffGram);
            string xmlDTDiffGramNoDS = sw.ToString();

            sw.GetStringBuilder().Length = 0;
            dtMain.WriteXml(sw, XmlWriteMode.WriteSchema);
            string xmlDTWriteSchemaNoDS = sw.ToString();

            Assert.Equal(xmlDSNone, xmlDTNoneInDS);

            // The only difference between the xml output from inside the
            // dataset and the xml output from outside the dataset is that
            // there's a fake <DocumentElement> tag surrounding tbe table
            // in the second case.  We replace it with the name of the
            // dataset for testing purposes.
            doc.LoadXml(xmlDTNoneNoDS);
            Assert.Equal("DocumentElement", doc.DocumentElement.Name);
            sw.GetStringBuilder().Length = 0;
            doc.Save(sw);
            xmlDTNoneNoDS = sw.ToString();
            xmlDTNoneNoDS = xmlDTNoneNoDS.Replace("<DocumentElement>", "<MyDataSet>");
            xmlDTNoneNoDS = xmlDTNoneNoDS.Replace("</DocumentElement>", "</MyDataSet>");

            StandardizeXmlFormat(ref xmlDSNone);

            Assert.Equal(xmlDSNone, xmlDTNoneNoDS);

            // Now check the DiffGram.
            Assert.Equal(xmlDSDiffGram, xmlDTDiffGramInDS);

            doc.LoadXml(xmlDTDiffGramNoDS);
            Assert.Equal("DocumentElement", doc.DocumentElement.FirstChild.Name);
            xmlDTDiffGramNoDS = xmlDTDiffGramNoDS.Replace("<DocumentElement>", "<MyDataSet>");
            xmlDTDiffGramNoDS = xmlDTDiffGramNoDS.Replace("</DocumentElement>", "</MyDataSet>");

            Assert.Equal(xmlDSDiffGram, xmlDTDiffGramNoDS);

            // Finally we check the WriteSchema version of the data.  First
            // we remove the extra "msdata:MainDataTable" attribute from
            // the schema declaration part of the DataTable xml.
            doc = new XmlDocument();
            doc.LoadXml(xmlDTWriteSchemaInDS);
            node = doc.DocumentElement.FirstChild.FirstChild;
            a = (XmlAttribute)node.Attributes.GetNamedItem("msdata:MainDataTable");
            Assert.NotNull(a);
            Assert.Equal("Main", a.Value);
            node.Attributes.Remove(a);
            sw.GetStringBuilder().Length = 0;
            doc.Save(sw);
            xmlDTWriteSchemaInDS = sw.ToString();

            StandardizeXmlFormat(ref xmlDSWriteSchema);

            Assert.Equal(xmlDSWriteSchema, xmlDTWriteSchemaInDS);

            // Remove the extra "msdata:MainDataTable" for the other test case.
            // Also make sure we have "NewDataSet" in the appropriate locations.
            doc = new XmlDocument();
            doc.LoadXml(xmlDTWriteSchemaNoDS);
            node = doc.DocumentElement.FirstChild.FirstChild;
            a = (XmlAttribute)node.Attributes.GetNamedItem("msdata:MainDataTable");
            Assert.NotNull(a);
            Assert.Equal("Main", a.Value);
            node.Attributes.Remove(a);
            sw.GetStringBuilder().Length = 0;
            doc.Save(sw);

            Assert.Equal("NewDataSet", doc.DocumentElement.Name);
            Assert.Equal("NewDataSet", doc.DocumentElement.FirstChild.Attributes["id"].Value);
            Assert.Equal("NewDataSet", doc.DocumentElement.FirstChild.FirstChild.Attributes["name"].Value);

            xmlDTWriteSchemaNoDS = sw.ToString();

            xmlDTWriteSchemaNoDS = xmlDTWriteSchemaNoDS.Replace("<NewDataSet>", "<MyDataSet>");
            xmlDTWriteSchemaNoDS = xmlDTWriteSchemaNoDS.Replace("</NewDataSet>", "</MyDataSet>");
            xmlDTWriteSchemaNoDS = xmlDTWriteSchemaNoDS.Replace("\"NewDataSet\"", "\"MyDataSet\"");

            Assert.Equal(xmlDSWriteSchema, xmlDTWriteSchemaNoDS);
        }