System.Data.Tests.DataTableReadWriteXmlTest.TestReadXml C# (CSharp) Метод

TestReadXml() приватный Метод

private TestReadXml ( ) : void
Результат void
        public void TestReadXml()
        {
            // For reading, DataTable.ReadXml only supports reading in xml with
            // the schema included.  This means that we can only read in XML
            // that was generated with the WriteSchema flag.  
            DataSet ds;
            DataTable dtMainInDS, dtChildInDS, dtMain;

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

            StringWriter sw = new StringWriter();

            // 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 xmlMultiTable = sw.ToString();

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

            DataTable newdt = new DataTable();

            try
            {
                newdt.ReadXml(new StringReader(xmlDTNone));
                Assert.False(true);
            }
            catch (InvalidOperationException)
            {
                // DataTable does not support schema inference from Xml.
            }

            try
            {
                newdt.ReadXml(new StringReader(xmlDTDiffGram));
                Assert.False(true);
            }
            catch (InvalidOperationException)
            {
                // DataTable does not support schema inference from Xml.
            }

            DataTable multiTable = new DataTable();
            multiTable.ReadXml(new StringReader(xmlMultiTable));
            // Do some simple checks to see if the main dataset was created
            // and if there are relationships present.
            Assert.Equal("MyDataSet", multiTable.DataSet.DataSetName);
            Assert.Equal(1, multiTable.ChildRelations.Count);
            Assert.Equal(1, multiTable.Constraints.Count);
            // Write the table back out and check to see that the XML is
            // the same as before.
            sw.GetStringBuilder().Length = 0;
            multiTable.WriteXml(sw, XmlWriteMode.WriteSchema, true);
            string xmlMultiTableCheck = sw.ToString();
            Assert.True(xmlMultiTable.IndexOf("UseCurrentLocale") > 0);
            Assert.True(xmlMultiTable.IndexOf("keyref") > 0);
            Assert.Equal(xmlMultiTable, xmlMultiTableCheck);

            DataTable singleTable = new DataTable();
            singleTable.ReadXml(new StringReader(xmlSingleTable));
            // Do some simple checks on the table.
            Assert.Null(singleTable.DataSet);
            Assert.Equal("Main", singleTable.TableName);
            // Write the table out and check if it's the same.
            sw.GetStringBuilder().Length = 0;
            singleTable.WriteXml(sw, XmlWriteMode.WriteSchema);
            string xmlSingleTableCheck = sw.ToString();
            Assert.Equal(xmlSingleTable, xmlSingleTableCheck);
        }
    }