Xml和数据集问题

时间:2015-07-20 21:22:29

标签: c# xml

这个问题很可能非常简单,但是我正在创建一个数据集并尝试使用XMLTextWriter将其保存到文件中

现在当我保存我的数据集然后尝试读取它时会读取表名但是将行显示为0?所以我的作家似乎没有写表格的行?

任何人都知道如何解决这个问题?

public static DataSet liveData = new DataSet();

    public static DataTable players = new DataTable("Players");

public static void WriteSchemaWithXmlTextWriter()
    {
        // Set the file path and name. Modify this for your purposes. 
        string filename = "Schema.xml";

        // Create a FileStream object with the file path and name.
        System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Create);

        // Create a new XmlTextWriter object with the FileStream.
        System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.Unicode);

        // Write the schema into the DataSet and close the reader.
        liveData.WriteXmlSchema(writer);
        writer.Close();
    }

    public static void ReadSchemaFromXmlTextReader()
    {
        // Set the file path and name. Modify this for your purposes. 
        string filename = "Schema.xml";

        // Create a FileStream object with the file path and name.
        System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open);

        // Create a new XmlTextReader object with the FileStream.
        System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stream);

        // Read the schema into the DataSet and close the reader.
        liveData.ReadXmlSchema(xmlReader);
        Console.WriteLine("y: {0}", liveData.Tables["Players"].Rows.Count);
        xmlReader.Close();
    }

提前感谢您的帮助:)

1 个答案:

答案 0 :(得分:2)

您正在编写架构,这是Xml的布局/结构,而不是实际内容。

您需要使用DataSet.WriteXml ...

https://msdn.microsoft.com/en-us/library/ms135426%28v=vs.110%29.aspx

......相反!