当前位置: 首页>>代码示例>>C#>>正文


C# DataTable.WriteXml方法代码示例

本文整理汇总了C#中DataTable.WriteXml方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.WriteXml方法的具体用法?C# DataTable.WriteXml怎么用?C# DataTable.WriteXml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataTable的用法示例。


在下文中一共展示了DataTable.WriteXml方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestReadXml

        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);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:82,代码来源:DataTableReadWriteXmlTest.cs

示例2: XmlTest31

        public void XmlTest31()
        {
            DataSet ds = new DataSet();
            DataTable parent = new DataTable("Parent");
            parent.Columns.Add(new DataColumn("col1", typeof(int)));
            parent.Columns.Add(new DataColumn("col2", typeof(string)));
            parent.Columns[0].Unique = true;

            DataTable child1 = new DataTable("Child1");
            child1.Columns.Add(new DataColumn("col3", typeof(int)));
            child1.Columns.Add(new DataColumn("col4", typeof(string)));
            child1.Columns.Add(new DataColumn("col5", typeof(int)));
            child1.Columns[2].Unique = true;

            DataTable child2 = new DataTable("Child2");
            child2.Columns.Add(new DataColumn("col6", typeof(int)));
            child2.Columns.Add(new DataColumn("col7"));

            parent.Rows.Add(new object[] { 1, "P_" });
            parent.Rows.Add(new object[] { 2, "P_" });

            child1.Rows.Add(new object[] { 1, "C1_", 3 });
            child1.Rows.Add(new object[] { 1, "C1_", 4 });
            child1.Rows.Add(new object[] { 2, "C1_", 5 });
            child1.Rows.Add(new object[] { 2, "C1_", 6 });

            child2.Rows.Add(new object[] { 3, "C2_" });
            child2.Rows.Add(new object[] { 3, "C2_" });
            child2.Rows.Add(new object[] { 4, "C2_" });
            child2.Rows.Add(new object[] { 4, "C2_" });
            child2.Rows.Add(new object[] { 5, "C2_" });
            child2.Rows.Add(new object[] { 5, "C2_" });
            child2.Rows.Add(new object[] { 6, "C2_" });
            child2.Rows.Add(new object[] { 6, "C2_" });

            ds.Tables.Add(parent);
            ds.Tables.Add(child1);
            ds.Tables.Add(child2);

            DataRelation relation = new DataRelation("Relation1", parent.Columns[0], child1.Columns[0]);
            parent.ChildRelations.Add(relation);

            relation = new DataRelation("Relation2", child1.Columns[2], child2.Columns[0]);
            child1.ChildRelations.Add(relation);

            using (FileStream stream = new FileStream(_tempFile, FileMode.Create))
            {
                parent.WriteXml(stream, XmlWriteMode.WriteSchema, true);
            }

            DataTable table = new DataTable();
            table.ReadXml(_tempFile);

            Assert.Equal("Parent", table.TableName);
            Assert.Equal("NewDataSet", table.DataSet.DataSetName);
            Assert.Equal(2, table.Columns.Count);
            Assert.Equal(2, table.Rows.Count);
            Assert.Equal(typeof(int), table.Columns[0].DataType);
            Assert.Equal(typeof(string), table.Columns[1].DataType);
            Assert.Equal(1, table.Constraints.Count);
            Assert.Equal(typeof(UniqueConstraint), table.Constraints[0].GetType());
            Assert.Equal(1, table.ChildRelations.Count);
            Assert.Equal("Relation1", table.ChildRelations[0].RelationName);
            Assert.Equal("Parent", table.ChildRelations[0].ParentTable.TableName);
            Assert.Equal("Child1", table.ChildRelations[0].ChildTable.TableName);

            DataTable table1 = table.ChildRelations[0].ChildTable;
            Assert.Equal("Child1", table1.TableName);
            Assert.Equal("NewDataSet", table1.DataSet.DataSetName);
            Assert.Equal(3, table1.Columns.Count);
            Assert.Equal(4, table1.Rows.Count);
            Assert.Equal(typeof(int), table1.Columns[0].DataType);
            Assert.Equal(typeof(string), table1.Columns[1].DataType);
            Assert.Equal(typeof(int), table1.Columns[2].DataType);
            Assert.Equal(2, table1.Constraints.Count);
            Assert.Equal(typeof(UniqueConstraint), table1.Constraints[0].GetType());
            Assert.Equal(typeof(ForeignKeyConstraint), table1.Constraints[1].GetType());
            Assert.Equal(1, table1.ParentRelations.Count);
            Assert.Equal(1, table1.ChildRelations.Count);
            Assert.Equal("Relation1", table1.ParentRelations[0].RelationName);
            Assert.Equal("Relation2", table1.ChildRelations[0].RelationName);
            Assert.Equal("Parent", table1.ParentRelations[0].ParentTable.TableName);
            Assert.Equal("Child2", table1.ChildRelations[0].ChildTable.TableName);

            table1 = table1.ChildRelations[0].ChildTable;
            Assert.Equal("Child2", table1.TableName);
            Assert.Equal("NewDataSet", table1.DataSet.DataSetName);
            Assert.Equal(2, table1.Columns.Count);
            Assert.Equal(8, table1.Rows.Count);
            Assert.Equal(typeof(int), table1.Columns[0].DataType);
            Assert.Equal(typeof(string), table1.Columns[1].DataType);
            Assert.Equal(1, table1.Constraints.Count);
            Assert.Equal(typeof(ForeignKeyConstraint), table1.Constraints[0].GetType());
            Assert.Equal(1, table1.ParentRelations.Count);
            Assert.Equal(0, table1.ChildRelations.Count);
            Assert.Equal("Relation2", table1.ParentRelations[0].RelationName);
            Assert.Equal("Child1", table1.ParentRelations[0].ParentTable.TableName);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:98,代码来源:DataTableTest4.cs


注:本文中的DataTable.WriteXml方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。