2015-07-20 53 views
2

这个问题是最有可能很简单,但我想提出一个数据集,并尝试使用的XmlTextWriter问题XML和数据集

现在,当我救我的数据集将其保存到一个文件,然后尝试读取它会读取表名称,但显示行为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(); 
    } 

感谢您对您的帮助提前:)

+0

什么是'liveData'? – EZI

+0

道歉我切断了一些我的代码:) – Marca

+0

public static DataSet liveData = new DataSet(); public static DataTable players = new DataTable(“Players”); – Marca

回答

2

你正在写的模式,这是的XML的布局/结构,而不是实际的内容。

您需要使用DataSet.WriteXml ...

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

...而不是!

+0

这么简单,当你使用正确的东西:)现在有一种方法来编码这个,所以它不能在外部修改?谢谢大家 – Marca

+0

编码是什么? XML文件/数据?如果你想让人们阅读它,那么恐怕他们将能够修改它!你可以加密一个文件,但是人们需要密码来访问它,然后他们可以修改它。 –