-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLMethods.cs
More file actions
128 lines (77 loc) · 3.29 KB
/
XMLMethods.cs
File metadata and controls
128 lines (77 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace GuitarExercises
{
public class xmlMethods
{
public static void InsertObject(ExerciseObject obj)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<ExerciseObject>));
bool dataExists = File.Exists("myExercises.xml");
// Declare an object variable of the type to be deserialized.
List<ExerciseObject> data = new List<ExerciseObject>();
if (dataExists)
{
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream("myExercises.xml", FileMode.Open);
/* Use the Deserialize method to restore the object's state with
data from the XML document. */
data = (List<ExerciseObject>)serializer.Deserialize(fs);
fs.Close();
}
data.Add(obj);
TextWriter writer = new StreamWriter("myExercises.xml");
serializer.Serialize(writer, data);
writer.Close();
}
public static int NextIndex(List<ExerciseObject> list)
{
int toReturn = list.Count;
return toReturn;
}
public static ExerciseObject GetRandom()
{
XmlSerializer serializer = new XmlSerializer(typeof(List<ExerciseObject>));
FileStream fs = new FileStream("myExercises.xml", FileMode.Open);
// Declare an object variable of the type to be deserialized.
List<ExerciseObject> data = new List<ExerciseObject>();
/* Use the Deserialize method to restore the object's state with
data from the XML document. */
data = (List<ExerciseObject>)serializer.Deserialize(fs);
fs.Close();
var rand = new Random();
int ranIndex = rand.Next(data.Count);
ExerciseObject toReturn = data.ElementAt(ranIndex);
return toReturn;
}
public static void UpdateExisting(ExerciseObject currentItem)
{
//Read DB File
XmlSerializer serializer = new XmlSerializer(typeof(List<ExerciseObject>));
FileStream fs = new FileStream("myExercises.xml", FileMode.Open);
// Declare an object variable of the type to be deserialized.
List<ExerciseObject> data = new List<ExerciseObject>();
/* Use the Deserialize method to restore the object's state with
data from the XML document. */
data = (List<ExerciseObject>)serializer.Deserialize(fs);
fs.Close();
foreach (ExerciseObject thingie in data)
{
if (thingie.NameOfExercise.Equals(currentItem.NameOfExercise))
{
thingie.Tempo = currentItem.Tempo;
thingie.Description = currentItem.Description;
}
}
//update db file
TextWriter writer = new StreamWriter("myExercises.xml");
serializer.Serialize(writer, data);
writer.Close();
}
}
}