Last active
December 26, 2015 08:15
-
-
Save yalayabeeb/091354eade770aba21d2 to your computer and use it in GitHub Desktop.
A class to save and read data to an XML file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.IO; | |
using System.Xml; | |
public class Data | |
{ | |
public readonly XmlDocument Document; | |
private readonly string _filePath; | |
private string CorePath { get; set; } | |
private string ElementPath { get; set; } | |
public Data(string filePath, string corePath, string elementPath) | |
{ | |
CorePath = corePath; | |
ElementPath = elementPath; | |
Document = new XmlDocument(); | |
_filePath = filePath; | |
string directory = Path.GetDirectoryName(filePath); | |
bool existing = false; | |
while (!existing) | |
{ | |
if (!Directory.Exists(directory)) | |
{ | |
Directory.CreateDirectory(directory); | |
} | |
existing = Directory.Exists(Path.GetDirectoryName(directory)); | |
directory = Path.GetDirectoryName(directory); | |
} | |
if (!File.Exists(_filePath)) | |
{ | |
File.WriteAllText(_filePath, string.Format(@"<{0}></{0}>", corePath)); | |
} | |
else if (string.IsNullOrEmpty(File.ReadAllText(_filePath))) | |
{ | |
File.WriteAllText(_filePath, string.Format(@"<{0}></{0}>", corePath)); | |
} | |
} | |
public XmlNode CreateNode(string key, string value) | |
{ | |
var node = Document.CreateElement(key); | |
node.InnerText = value; | |
return node; | |
} | |
public bool Save(XmlNode[] nodes) | |
{ | |
Document.Load(_filePath); | |
var mainElement = Document.CreateElement(ElementPath); | |
foreach (var node in nodes) | |
{ | |
mainElement.AppendChild(node); | |
} | |
Document.DocumentElement.AppendChild(mainElement); | |
Document.Save(_filePath); | |
return true; | |
} | |
public IEnumerable<XmlNode> Read() | |
{ | |
Document.Load(_filePath); | |
foreach (XmlNode node in Document.SelectNodes(string.Format("{0}/{1}", CorePath, ElementPath))) | |
{ | |
yield return node; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Xml; | |
class Program_Example | |
{ | |
static void Main(string[] args) | |
{ | |
string storageDir = Environment.CurrentDirectory + @"\List of Games.xml"; | |
Data data = new Data(storageDir, "Games", "Game"); | |
var nodeName = data.CreateNode("Name", "Call of Duty"); | |
var nodeValue = data.CreateNode("Value", "10.00"); | |
data.Save(new XmlNode[] { nodeName, nodeValue }); | |
var nodeList = data.Read(); | |
foreach (var node in nodeList) | |
{ | |
Console.WriteLine(node.SelectSingleNode("Name").InnerText); | |
Console.WriteLine(node.SelectSingleNode("Value").InnerText); | |
} | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment