2015-07-20 103 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文件/數據?如果你想讓人們閱讀它,那麼恐怕他們將能夠修改它!你可以加密一個文件,但是人們需要密碼來訪問它,然後他們可以修改它。 –