0
我正在處理一個應用程序,並將數據保存爲XML格式,但我遇到了格式正確的問題。使用XmlWriter創建XML時出現錯誤「<NodeName>」
輸出看起來像使用此此
<GasInfoEntries>
<Gallons>123</Gallons>
<Price>456</Price>
</GasInfoEntries><GasInfoEntries>
<Gallons>123</Gallons>
<Price>456</Price>
</GasInfoEntries>
IM把它寫
List<GasInfoEntries> data = new List<GasInfoEntries>();
data.Add(new GasInfoEntries() { Gallons = TxtBoxGas.Text, Price = TxtBoxPrice.Text });
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("GasInfoEntries");
xmlWriter.WriteStartElement("Gallons", "");
xmlWriter.WriteString(TxtBoxGas.Text);
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("Price", "");
xmlWriter.WriteString(TxtBoxPrice.Text);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
//do i need?
xmlWriter.Close();
我得到這個錯誤
- $exception
{System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: <GasInfoEntries xmlns=''> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read3_ArrayOfGasInfoEntries()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, Object events)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
at LP_Buddy.MainPage.btnShow_Click(Object sender, RoutedEventArgs e)}
System.Exception {System.InvalidOperationException}
什麼想法?
謝謝
我必須在後面的代碼中聲明它嗎?像xmlWriter.WriteStartElement(「GasRoot」);謝謝 – branedge
以及第二個想法...仍然有一些問題。將有可能添加和刪除手機上的XML文件的記錄,或者我應該切換到數據庫? – branedge
@ user2712080當然,您必須將其明確定義爲任何其他元素。從XML中刪除元素當然是可能的,但這需要重寫整個文件(如果您打算使用XmlWriter)。如果你打算更新/更改文件中的數據,那麼數據庫肯定是更好的解決方案。 –