这个问题很可能非常简单,但我正在制作一个数据集并尝试使用 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();
}
感谢您提前提供的帮助:)
最佳答案
您正在编写架构,即 Xml 的布局/结构,而不是实际内容。
您需要使用DataSet.WriteXml
...
https://msdn.microsoft.com/en-us/library/ms135426%28v=vs.110%29.aspx
...相反!
关于c# - Xml 和数据集的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31526588/