Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:59:28+00:00 2026-06-02T07:59:28+00:00

Currently working on a wp7 App, its quite basic. the user has a counter

  • 0

Currently working on a wp7 App, its quite basic. the user has a counter and if a date element exists for the current day in an XML file the count is updated, if not a new date element is created for that day and with the count as the value.

My priblem is, all is working fine if a new XML file is created, the current date element is updated no problem, but if I test the following day, a new element is created, but when I want to update the count, a new date element is added. I don’t get this as all the code works on a new file, but if the file is a day old its not for some reason.

XML code

<?xml version="1.0" encoding="utf-8"?>
<Countlog>
 <date Count="9">4/21/2012</date>
 <date Count="4">4/21/2012</date>
 <date Count="18">4/21/2012</date>
</Countlog>

C#

private void save_btn_Click(object sender, RoutedEventArgs e)
    {
        String _count = Count_tb.Text;
        String s_todaysdate = todaysdate.Date.ToShortDateString();

        IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("Countlog.xml", FileMode.Open, myIsolatedStorage);
        StreamReader reader = new StreamReader(isoStream);
        XDocument _xml = XDocument.Load(reader);
        isoStream.Close();

        var query = from r in _xml.Descendants("Countlog")
                    where r.Element("date").Value == (DateTime.Now.ToShortDateString())
                    select r.Element("date");


        if (!query.Any())
        {
            XElement Addnewdate = new XElement("date", s_todaysdate, new XAttribute("Count", _count));
            _xml.Root.Add(Addnewdate);
            MessageBox.Show("no matching date");
        }
        else
        {
            foreach (XElement _date in query)
            {
                _date.Attribute("Count").Value = _count.ToString();
                MessageBox.Show("Updating date");

            }
        }

        IsolatedStorageFileStream isoStreamsave = new IsolatedStorageFileStream("Countlog.xml", FileMode.Truncate, myIsolatedStorage);

        _xml.Save(isoStreamsave);
        isoStreamsave.Close();
    }





    private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (pivotholder.SelectedIndex == 1)
        {
            IsolatedStorageFileStream isoFileStream2 = myIsolatedStorage.OpenFile("Countlog.xml", FileMode.Open);
            StreamReader reader = new StreamReader(isoFileStream2);
            XML_result.Text = reader.ReadToEnd();
            reader.Close();
        }

    }

Please let me know if you need more info, This is my first time posting here after lurking around for the past few years.

Cheers

Jon

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:59:29+00:00Added an answer on June 2, 2026 at 7:59 am

    Okay, I’ve worked it out. This query:

    var query = from r in _xml.Descendants("Countlog")
                where r.Element("date").Value == (DateTime.Now.ToShortDateString())
                select r.Element("date");
    

    will only match if the first date element has the right value. You’re iterating over all Countlog elements (of which there’s only one), and looking for the first date element (because that’s what Element(...) does).

    You could change this to use simply:

    var query = _xml.Root.Elements("date")
                         .Where(x => x.Value == (DateTime.Now.ToShortDateString())
    

    However, I would suggest an alternative format to start with:

    var date = DateTime.Today;
    var query = _xml.Root.Elements("date")
                    .Where(x => (DateTime) x.Value == date);
    

    Then to add a new element:

    XElement element = new XElement("date",
                          new XAttribute("count", count),
                          date);
    

    Or to update one:

    element.Attribute("count").SetValue(count);
    

    This uses the data-type handling of LINQ to XML, instead of converting everything to strings explicitly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently working on a Spring 2.5 web application, looks as if the business has
Im currently working on an app which lists a set of objects in a
I currently working on a project which uses Spree Cart and which has hence
Im currently working on a Python/Pygame module to wrap some basic sprite animation. Animation
I am working on a localized wp7 app and in the process of building
Were currently working on a script that reads the current users information from out
Developing a WP7 app, and I'm trying to post something to the user's feed.
Currently working on a snippet to input variables in which a user changes a
Im currently working on a timer app similar to apple's default Clock utility but
Im currently working on a mobile web app and am wondering if I should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.