2012-09-09 107 views
0

该应用程序应该不时添加节点到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> 

或者如何在节点追加不说字符串被自动添加?

回答

2

反序列化您的数据,添加新值并序列化。但是 使用FileMode.Create而不是FileMode.Append

+0

+1。 Read-add-save是安全的方法。如果OP想要继续附加数据,则添加另一个答案。 –

+0

因此以及如何将新节点添加到反序列化的xml中。 Im在XmlSerializer serializer = new XmlSerializer(typeof(List )); (列表)serializer.Deserialize(stream);如何使用这个变量数据? –

+0

@AntonMashikhin'如何将新节点附加到反序列化xml'。不需要进行xml处理。反序列化后,你有'数据'。将新目标添加到'data'并序列化它 –

2

生成的文件是无效的XML,因此您将无法将其直接用于反序列化作为有效的Xml。

但它实际上是有效的“Xml片段”,可以用标准类读取:当在XmlReader.Create调用的XmlReaderSettings中指定ConformanceLevel.Frament时,XmlReader可以读取片段。我想你甚至可以直接从这样的阅读器反序列化类(不确定)。注意:读取旧数据会更容易(但更少的问题和错误),将您需要的内容和序列化作为整个文件追溯。

0
XmlRootAttribute root = new XmlRootAttribute("Goals");  
XmlSerializer serializer = new XmlSerializer(typeof(List<Goals>), root); 
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
ns.Add(string.Empty, string.Empty); 
using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings)) 
{ 
    serializer.Serialize(xmlWriter, GenerateGoalsData(name, description, progress), ns) 
}