Jump to content

Recommended Posts

Posted

Oi.  Estou a fazer uma aplicação em C# para windows phone e preciso de guardar alguns dados. Disseram-me que era melhor guardar em XML. Como nunca guardei em XML, fiz umas pesquisas e experimentei fazer da seguinte forma:

public void guardarJogo()
        {
            XmlWriter w = XmlWriter.Create("jogoGuardado.xml");
            w.WriteStartElement("Jogo");
            w.WriteElementString("Atributo", "");
            w.WriteEndElement();
            w.Close();
        }

        public void abrirJogoGuardado()
        {
            string word;
            XmlReader r = XmlReader.Create("jogoGuardado.xml");
            r.ReadStartElement("Jogo");
            word = r.ReadElementContentAsString();
            r.Close();
        }

Acontece que no método guardarJogo() aparece um erro relacionado com o parâmetro do XmlWriter.Create("jogoGuardado.xml") . Alguém me sabe dizer como devo resolver o problema? thanks

Este código encontrei em http://social.msdn.microsoft.com/Forums/es-ES/vcses/thread/ba2aadd5-96cf-4964-97e5-b1369ff9b2c5/

Posted

Diz: "The best overloaded method match for 'System.Xml.XmlWritter.Create(System.IO.Stream)' has some invalid arguments"

tenho que converter a string para aquele tipo mas não aceita ("jogoGuardado.xml") as Stream

Posted

Também já tentei criar um filestream, como se pode ver em baixo, mas também não dá, aparece uma mensagem a diz:

An unhandled exception of type 'System.MethodAccessException' occurred in SlXnaApp1.dll

Additional information: Attempt to access the method failed: System.IO.FileStream..ctor(System.String, System.IO.FileMode)

        public void guardarJogo()
        {
            FileStream fich = new FileStream("jogoGuardado.xml",FileMode.Create);
            XmlWriter w = XmlWriter.Create(fich);
            w.WriteStartElement("Jogo");
            w.WriteElementString("Atributo", "");
            w.WriteEndElement();
            w.Close();
        }
Posted

Parece que já funciona  😁 petvetbr usei o link que indicaste e fiz

        public void guardarJogo()
        {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("jogoGuardado.xml", FileMode.Create, myIsolatedStorage);
            XmlWriter w = XmlWriter.Create(isoStream);
            w.WriteStartElement("Jogo");
            w.WriteElementString("Atributo", "");
            w.WriteEndElement();
            w.Close();
        }

erros já não dá, resta saber se vai mesmo guardar o que quero mas disso já trato. Obrigado aos dois pelas dicas  😄

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.