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 7166967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:29:59+00:00 2026-05-28T14:29:59+00:00

For a scoreboard, i store the scores in an xml file. (WPF application) Of

  • 0

For a scoreboard, i store the scores in an xml file. (WPF application)
Of course, i dont want a user to just edit that xml file, so i was looking for a way to encrypt the xml file.
this works trough the method i found here: http://srinivasganaparthi.blogspot.com/2011/04/encrypt-and-decrypt-xml-file-in-c.html

So, i have put this in a class file in my project:

public static void EncryptAndSerialize(Object obj)
{
    UnicodeEncoding aUE = new UnicodeEncoding();
    byte[] key = aUE.GetBytes("password");
    RijndaelManaged RMCrypto = new RijndaelManaged();
    using (FileStream fs = File.Open(@"ScoreData.xml", FileMode.Create))
    {
        using (CryptoStream cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write))
        {
            XmlSerializer xmlser = new XmlSerializer(obj.GetType());
            xmlser.Serialize(cs, obj);
        }
        fs.Close();
    }
}

public static DataSet DecryptAndDeserialize(string filename)
{
    DataSet ds = new DataSet();
    FileStream aFileStream = new FileStream(filename, FileMode.Open);
    StreamReader aStreamReader = new StreamReader(aFileStream);
    UnicodeEncoding aUE = new UnicodeEncoding();
    byte[] key = aUE.GetBytes("password");
    RijndaelManaged RMCrypto = new RijndaelManaged();
    CryptoStream aCryptoStream = new CryptoStream(aFileStream, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);

    //Restore the data set to memory.
    ds.ReadXml(aCryptoStream);
    aStreamReader.Close();
    aFileStream.Close();
    return ds;
}

And it works very good indeed, the XML file gets encrypted in the part where i write the xml file here:

private void saveScore_Click(object sender, RoutedEventArgs e)
{
    if (!File.Exists("ScoreData.xml")) //als file nog niet bestaat
    {
        XmlTextWriter textWritter = new XmlTextWriter("ScoreData.xml", null);
        textWritter.WriteStartDocument();
        textWritter.WriteStartElement("Data");

        textWritter.WriteEndElement();
        textWritter.Close();

    }



    XmlDocument xmlDoc = new XmlDocument();
    dweMethods.DecryptAndDeserialize("ScoreData.xml");
    xmlDoc.Load("ScoreData.xml");

    XmlElement subRoot = xmlDoc.CreateElement("Persons");
    //Naam
    XmlElement appendedElementNaam = xmlDoc.CreateElement("Name");
    XmlText xmlTextNaam = xmlDoc.CreateTextNode(nameOfPerson.Text);
    appendedElementNaam.AppendChild(xmlTextNaam);
    subRoot.AppendChild(appendedElementNaam);
    xmlDoc.DocumentElement.AppendChild(subRoot);
    //Score
    XmlElement appendedElementScore = xmlDoc.CreateElement("Score");
    XmlText xmlTextScore = xmlDoc.CreateTextNode(Convert.ToString(endScore));
    appendedElementScore.AppendChild(xmlTextScore);
    subRoot.AppendChild(appendedElementScore);
    xmlDoc.DocumentElement.AppendChild(subRoot);
    //Date
    XmlElement appendedElementDate = xmlDoc.CreateElement("Date");
    XmlText xmlTextDate = xmlDoc.CreateTextNode(DateTime.Now.ToString("d/M/yyyy"));
    appendedElementDate.AppendChild(xmlTextDate);
    subRoot.AppendChild(appendedElementDate);
    xmlDoc.DocumentElement.AppendChild(subRoot);


    xmlDoc.Save("ScoreData.xml");
    dweMethods.EncryptAndSerialize("ScoreData.xml");


}

But here comes the part i just dont understand how to do:
To view the “scoreboard”, the user opens a window were a datagrid is, where the datagrid just reads the xml file.
Now, because the xml file is encrypted, the datagrid can’t just read the xml file.
(also here above, when it loads the xml, it has the same issue)

The reading is done like this:

public ScoreBoard()
{
    InitializeComponent();
    dweMethods.DecryptAndDeserialize("ScoreData.xml");
    XElement TrackList = XElement.Load("ScoreData.xml");
    LibraryView.DataContext = TrackList;


}

in that second line i have dweMethods.DecryptAndDeserialize("ScoreData.xml"); but im not sure what to bind it to, and then how i can make the Xelement.Load, load the decrypted file.
The code above, makes the application crash now (of course) because it tries to read an unreadable xml file now.

Could anyone help me out a bit here?
I think im just forgetting some very small, but logical step here.

Thank you very much in advance.

========================

Edit1 (after first answer)

To open the xml to write to, the code is now like this (edited from above)
private void saveScore_Click(object sender, RoutedEventArgs e)

{
    if (!File.Exists("ScoreData.xml")) //als file nog niet bestaat
    {
        XmlTextWriter textWritter = new XmlTextWriter("ScoreData.xml", null);
        textWritter.WriteStartDocument();
        textWritter.WriteStartElement("Data");

        textWritter.WriteEndElement();
        textWritter.Close();
        dweMethods.EncryptAndSerialize("ScoreData.xml");

    }



    XmlDocument xmlDoc = new XmlDocument();
    DataSet ds = dweMethods.DecryptAndDeserialize("ScoreData.xml");
    xmlDoc.Load(ds.GetXml());

    XmlElement subRoot = xmlDoc.CreateElement("Persons");
    //Naam
    XmlElement appendedElementNaam = xmlDoc.CreateElement("Name");
    XmlText xmlTextNaam = xmlDoc.CreateTextNode(nameOfPerson.Text);
    appendedElementNaam.AppendChild(xmlTextNaam);
    subRoot.AppendChild(appendedElementNaam);
    xmlDoc.DocumentElement.AppendChild(subRoot);
    //Score
    XmlElement appendedElementScore = xmlDoc.CreateElement("Score");
    XmlText xmlTextScore = xmlDoc.CreateTextNode(Convert.ToString(endScore));
    appendedElementScore.AppendChild(xmlTextScore);
    subRoot.AppendChild(appendedElementScore);
    xmlDoc.DocumentElement.AppendChild(subRoot);
    //Date
    XmlElement appendedElementDate = xmlDoc.CreateElement("Date");
    XmlText xmlTextDate = xmlDoc.CreateTextNode(DateTime.Now.ToString("d/M/yyyy"));
    appendedElementDate.AppendChild(xmlTextDate);
    subRoot.AppendChild(appendedElementDate);
    xmlDoc.DocumentElement.AppendChild(subRoot);


    xmlDoc.Save("ScoreData.xml");
    dweMethods.EncryptAndSerialize("ScoreData.xml");


}

the error now is in the line xmlDoc.Load(ds.GetXml()); where it now says to have Illegal characters in path. but this should not be possible if it has been decrypted correct i guess :/

  • 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-05-28T14:30:00+00:00Added an answer on May 28, 2026 at 2:30 pm

    You can use XElement.Parse() to load xml from a string

    public ScoreBoard()
    {
        InitializeComponent();
        DataSet ds = dweMethods.DecryptAndDeserialize("ScoreData.xml")
        XElement TrackList = XElement.Parse(ds.GetXml());
        LibraryView.DataContext = TrackList;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF storyboard that I want to step through frame-by-frame (for a
I have a storyboard(1) that does some basic animations in 2 seconds. I want
I'm new to silverlight and wpf programming I've just created a simple storyboard in
A bit of background: I'm building an MVC app to store golf course data
I created an animation storyboard in xaml file. That story board begins on Button.Click.
I have a game that I am making and I want to add a
Im creating a scoreboard within flash, ive got mysql data converted to xml and
I'm looking to create the storyboard idea that oDesk uses on http://www.odesk.com/w/odesk_story I like
I have a nested Repeater control in the ItemTemplate of another Repeater. I want
I'm just wondering if there is a more efficient way of doing this. I

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.