該應用程序應該不時添加節點到Goals.xml
文件。所以它的dynamic
。這增加了節點的代碼:刪除XML中的多個XML聲明
XmlWriterSettings settings=new XmlWriterSettings();
settings.OmitXmlDeclaration= true;
settings.Indent = true;
settings.IndentChars = ("\t");
using (IsolatedStorageFile myIsolatedStorage =
IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream stream =
myIsolatedStorage.OpenFile("Goals.xml", FileMode.Append))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Goals>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings))
{
serializer.Serialize(
xmlWriter,
GenerateGoalsData(name, description, progress));
}
}
和
private List<Goals> GenerateGoalsData(
string name,
string description,
string progress)
{
List<Goals> data = new List<Goals>();
data.Add(new Goals() {
Name=name,
Description=description,
Progress=progress});
return data;
}
,也是我有Goals
類。但它會產生不好的XML
:
<ArrayOfGoals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Goals>
<Name>Jack</Name>
<Description>lalala</Description>
<Progress>97</Progress>
</Goals>
</ArrayOfGoals>
<ArrayOfGoals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Goals>
<Name>Taaaaaa</Name>
<Description>nanana</Description>
<Progress>50</Progress>
</Goals>
</ArrayOfGoals>
如何XML
刪除重複:
</ArrayOfGoals>
<ArrayOfGoals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
所以XML
看起來像這樣:
<ArrayOfGoals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Goals>
<Name>Jack</Name>
<Description>lalala</Description>
<Progress>97</Progress>
</Goals>
<Goals>
<Name>Taaaaaa</Name>
<Description>nanana</Description>
<Progress>50</Progress>
</Goals>
</ArrayOfGoals>
或者如何在節點追加不說字符串被自動添加?
+1。 Read-add-save是安全的方法。如果OP想要繼續附加數據,則添加另一個答案。 –
因此以及如何將新節點添加到反序列化的xml中。 Im在XmlSerializer serializer = new XmlSerializer(typeof(List)); (列表)serializer.Deserialize(stream);如何使用這個變量數據? –
@AntonMashikhin'如何將新節點附加到反序列化xml'。不需要進行xml處理。反序列化後,你有'數據'。將新目標添加到'data'並序列化它 –