0

The code is bellow...and I get the error at the last line : loadedData.Save("levelNo.xml");

And i don't understand why as there is a method with that type of parameter in XDocument : Save(String) Serialize this XDocument to a file, overwriting an existing file, if it exists.

Errors i get:
Error 2: Argument 1: cannot convert from 'string' to 'System.Xml.XmlWriter'
Error 1: The best overloaded method match for 'System.Xml.Linq.XDocument.Save(System.Xml.XmlWriter)' has some invalid arguments

XDocument loadedData = XDocument.Load("levelNo.xml", LoadOptions.None);
        XElement root = loadedData.Root;
        XElement asset = (XElement)root.FirstNode;
        asset.RemoveAll();
        asset.Add(new XElement("level", levelNo));
        asset.Add(new XElement("mana", player.Mana));
        asset.Add(new XElement("score", score));

        loadedData.Save("levelNo.xml");

I really need some help...I've been searchig for some hours for a solution...My goal is to modify the levelNo.xml file, and then rewrite it...

1 Answer 1

3

Where are you hoping to save the file to? In Windows Phone 7 you need to use isolated storage. There's a guide to WP7 Isolated Storage which has various examples, including reading and writing XML.

Ultimately, you'll need to open an IsolatedStorageFile, then create an IsolatedStorageFileStream, and write to that:

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var stream = store.CreateFile("levelNo.xml"))
    {
        loadedData.Save(stream);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hmmm thing is that i can easily read from an xml file, withouth using the isolated storage...Can't i do the same with writing?
@Teshte: Can you really? Have you actually got that working? Bear in mind that XDocument.Load can take a URI, which does make sense in WP7.
codeusing (System.Xml.XmlReader reader = System.Xml.XmlReader.Create("levelNo.xml")) { reader.MoveToContent(); reader.ReadToFollowing("level"); int lvl = Convert.ToInt32(reader.ReadInnerXml()); code This is how i read...bear in mind that i added the file to the place where i have all the other classes...not good programming i know..but the thing with the isolated storage and the emulator..from what i read..is that once you close the emulator, those data are lost..
@Teshte: That will compile, sure... but does it work? Where does it read the data from? (And would you expect to be able to write to the same storage area?)
I did that because it made testing easier from me...and that is why i wanted to do the xml writing from the initial post...however..i will try to do the isolated storage thing like you said...Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.