Reputation: 103
The app should add the nodes from time to time to Goals.xml
file. So its dynamic
. The code that adds the nodes:
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));
}
}
and
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;
}
and also I have class Goals
. But it generates bad 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>
How to remove in XML
the repeated:
</ArrayOfGoals>
<ArrayOfGoals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
so the XML
looks like that:
<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>
Or how to append the nodes without that string being automatically added?
Upvotes: 0
Views: 425
Reputation: 111
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)
}
Upvotes: 0
Reputation: 100547
The resulting file is invalid XML, so you will not be able to use it directly for deserialization as valid Xml.
But it is actually valid "Xml fragments" which can be read with standard classes: XmlReader can read fragments when specifying ConformanceLevel.Frament in XmlReaderSettings of XmlReader.Create call. I think you'll be even able to deserialize classes directly from such reader (not sure).
Side note: it would be much easier (but less problems and bugs) to read old data, append what you need and serialize back as whole file.
Upvotes: 2
Reputation: 116138
Deserialize your data, add new values and serialize. But
Use FileMode.Create
instead of FileMode.Append
Upvotes: 2