locked
XML Data Storage RRS feed

  • Question

  • I am building one of my first apps for personal testing and need some help.

    I am trying to build a simple baseball statistics app that store the data for a game in XML. I can get the app to write the xml the first time, but each time after that it gets overwritten with the new data. how can I get it to append the XML file to just add the new element data.

    here is an example of what I can currently do and what I would like to do.

    Thanks for any help I can get.

    <?xml version="1.0"?>

    <!--Baseball Statistics-->

    <?xml version="1.0"?>
    
    <!--Baseball Statistics-->
    -<GAMES>-<OPPONENT>
    <NAME>REDS</NAME>
    <DATE>9/17/2013</DATE>
    <AB>1</AB>
    <RBI>1</RBI>
    <R>0</R>
    <K>0</K>
    <BASE1>0</BASE1>
    <BASE2>0</BASE2>
    <BASE3>0</BASE3>
    <HR>0</HR>
    </OPPONENT>
    </GAMES>

    This is what I want.

    <?xml version="1.0"?>
    
    <!--Baseball Statistics-->
    -<GAMES>
    -<OPPONENT>
    <NAME>REDS</NAME>
    <DATE>9/17/2013</DATE>
    <AB>1</AB>
    <RBI>1</RBI>
    <R>0</R>
    <K>0</K>
    <BASE1>0</BASE1>
    <BASE2>0</BASE2>
    <BASE3>0</BASE3>
    <HR>0</HR>
    </OPPONENT>
    -<OPPONENT>
    <NAME>STEALTH</NAME>
    <DATE>9/18/2013</DATE>
    <AB>8</AB>
    <RBI>3</RBI>
    <R>0</R>
    <K>0</K>
    <BASE1>2</BASE1>
    <BASE2>2</BASE2>
    <BASE3>1</BASE3>
    <HR>0</HR>
    </OPPONENT>
    </GAMES>

    Tuesday, September 17, 2013 11:49 AM

Answers

  • Then try this, please mark it's not valid XML. I would recommend you to use SQLite database.

    var file = await ApplicationData.Current.LocalFolder.GetFileAsync("demo.xml");
    
    var readStream = await FileIO.ReadTextAsync(file);
    using (var writeStream = await file.OpenStreamForWriteAsync())
    {
        XElement xElem = XElement.Parse(readStream);
    
        xElem.Add(new XElement("GAMES", new XElement("OPPONENT",
            new XElement("NAME", "STEALTH"),
            new XElement("DATE", "9 / 18 / 2013"),
                new XElement("AB", "8"),
                new XElement("RBI", "3"),
                new XElement("R", "0"),
                new XElement("K", "0"),
                new XElement("BASE1", "2"),
                new XElement("BASE2", "2"),
                new XElement("BASE3", "1"),
                new XElement("HR", "0"))));
    
        xElem.Save(writeStream);
    }

    Wednesday, September 18, 2013 3:58 AM

All replies

  • You can do with the code given below.

    var file = await ApplicationData.Current.LocalFolder.GetFileAsync("demo.xml");
    
    var readStream = await FileIO.ReadTextAsync(file);
    using (var writeStream = await file.OpenStreamForWriteAsync())
    {
        XElement xElem = XElement.Parse(readStream);
    
        xElem.Add(new XElement("OPPONENT",
            new XElement("NAME", "STEALTH"),
            new XElement("DATE", "9 / 18 / 2013"),
                new XElement("AB", "8"),
                new XElement("RBI", "3"),
                new XElement("R", "0"),
                new XElement("K", "0"),
                new XElement("BASE1", "2"),
                new XElement("BASE2", "2"),
                new XElement("BASE3", "1"),
                new XElement("HR", "0")));
    
        xElem.Save(writeStream);
    }



    • Proposed as answer by Xyroid Tuesday, September 17, 2013 8:29 PM
    • Edited by Xyroid Tuesday, September 17, 2013 8:35 PM
    Tuesday, September 17, 2013 6:59 PM
  • <GAMES>-
    <OPPONENT>
    <NAME>REDS</NAME>
    <DATE>9/17/2013</DATE>
    <AB>1</AB>
    <RBI>1</RBI>
    <R>0</R>
    <K>0</K>
    <BASE1>0</BASE1>
    <BASE2>0</BASE2>
    <BASE3>0</BASE3>
    <HR>0</HR>
    </OPPONENT>
    </GAMES>
    <GAMES>-
    <OPPONENT>
    <NAME>REDS</NAME>
    <DATE>9/17/2013</DATE>
    <AB>1</AB>
    <RBI>1</RBI>
    <R>0</R>
    <K>0</K>
    <BASE1>0</BASE1>
    <BASE2>0</BASE2>
    <BASE3>0</BASE3>
    <HR>0</HR>
    </OPPONENT>
    </GAMES>

    Is there anyway to get it to separate the elements like this, with GAMES as the data separation.

    Thanks, otherwise works perfectly.


    Tuesday, September 17, 2013 7:45 PM
  • Do you want XML like this.

    <?xml version="1.0" encoding="utf-8"?>
    <GAMES>
      <OPPONENT>
        <NAME>REDS</NAME>
        <DATE>9/17/2013</DATE>
        <AB>1</AB>
        <RBI>1</RBI>
        <R>0</R>
        <K>0</K>
        <BASE1>0</BASE1>
        <BASE2>0</BASE2>
        <BASE3>0</BASE3>
        <HR>0</HR>
      </OPPONENT>
    </GAMES>
    
    <GAMES>
      <OPPONENT>
        <NAME>STEALTH</NAME>
        <DATE>9 / 18 / 2013</DATE>
        <AB>8</AB>
        <RBI>3</RBI>
        <R>0</R>
        <K>0</K>
        <BASE1>2</BASE1>
        <BASE2>2</BASE2>
        <BASE3>1</BASE3>
        <HR>0</HR>
      </OPPONENT>
    </GAMES>
    Tuesday, September 17, 2013 8:35 PM
  • That would be ideal. Would that work for using that as a datasourse eventually
    Tuesday, September 17, 2013 8:58 PM
  • Do you want XML like this.

    <?xml version="1.0" encoding="utf-8"?>
    <GAMES>
      <OPPONENT>
        <NAME>REDS</NAME>
        <DATE>9/17/2013</DATE>
        <AB>1</AB>
        <RBI>1</RBI>
        <R>0</R>
        <K>0</K>
        <BASE1>0</BASE1>
        <BASE2>0</BASE2>
        <BASE3>0</BASE3>
        <HR>0</HR>
      </OPPONENT>
    </GAMES>
    
    <GAMES>
      <OPPONENT>
        <NAME>STEALTH</NAME>
        <DATE>9 / 18 / 2013</DATE>
        <AB>8</AB>
        <RBI>3</RBI>
        <R>0</R>
        <K>0</K>
        <BASE1>2</BASE1>
        <BASE2>2</BASE2>
        <BASE3>1</BASE3>
        <HR>0</HR>
      </OPPONENT>
    </GAMES>

    How would that work? The GAME entity only has one opponent.

    The original design made more sense except there was no attribute for GAME to identify it. What you really want to be able to do is read in a GAME, update/add its elements and then write it out to storage, replacing the existing GAME. Or you could just use a proper database like SQL Light ;-)

    Tuesday, September 17, 2013 10:25 PM
  • Then try this, please mark it's not valid XML. I would recommend you to use SQLite database.

    var file = await ApplicationData.Current.LocalFolder.GetFileAsync("demo.xml");
    
    var readStream = await FileIO.ReadTextAsync(file);
    using (var writeStream = await file.OpenStreamForWriteAsync())
    {
        XElement xElem = XElement.Parse(readStream);
    
        xElem.Add(new XElement("GAMES", new XElement("OPPONENT",
            new XElement("NAME", "STEALTH"),
            new XElement("DATE", "9 / 18 / 2013"),
                new XElement("AB", "8"),
                new XElement("RBI", "3"),
                new XElement("R", "0"),
                new XElement("K", "0"),
                new XElement("BASE1", "2"),
                new XElement("BASE2", "2"),
                new XElement("BASE3", "1"),
                new XElement("HR", "0"))));
    
        xElem.Save(writeStream);
    }

    Wednesday, September 18, 2013 3:58 AM
  • I downloaded and installed SQLite from NuGet. I added the reference to my solution, but cannot get the code for the connection format correct.

    This is what I have.

    Private Sub DBconn()
    dim sqlconn as new SQLiteConnection("DataSourceXXXXXX")
    sqlconn.open()
    End Sub
    

    It does not like the sqlconn.open command.

    I tried it in a C# app as well.

    Wednesday, September 18, 2013 4:24 PM