- jonno_simpletestrunner_demo.zip
- Jonno Nunit Helper.AddIn
- Jonno.AddIns.Entities.dll
- Jonno.AddIns.Logic.VisualStudioSpecific.dll
- Jonno.AddIns.NunitHelper.dll
- Jonno.Extensions.dll
- Jonno.SimpleTestRunner.Entities.dll
- Jonno.SimpleTestRunner.exe
- Jonno.SimpleTestRunner.Logic.dll
- Jonno.Utilities.dll
- test framework helper options.xml
- jonno_simpletestrunner_src.zip
- Jonno Nunit Helper.AddIn
- Jonno.AddIns.Entities.UnitTests
- Jonno.AddIns.Entities
- Jonno.AddIns.Logic.EnvDTE
- Jonno.AddIns.NunitHelper
- Jonno.Extensions.UnitTests
- Jonno.Extensions
- Jonno.Logic.SimpleTestRunner
- Jonno.SimpleTestRunner.Entities
- Jonno.SimpleTestRunner
- Jonno.Utilities
- Nunit helper.sln
|
//-------------------------------------------------------------------------------------
// <copyright file='KeyValueXML.cs' company='Jonno'>
// Copyright (c) Paul Johnson 2009 (email:paulmichael.johnson@gmail.com)
// Code is released under The Code Project Open License (CPOL).
// </copyright>
//-------------------------------------------------------------------------------------
namespace Jonno.Utilities
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
internal class KeyValueXML : KeyValueOptions
{
public KeyValueXML(string name, string folderPath)
{
this.Name = name;
this.FolderPath = folderPath;
}
public string Name { private get; set; }
public string FolderPath { private get; set; }
public Dictionary<string, string> Load()
{
return this.LoadElements();
}
public void Save(Dictionary<string, string> options)
{
this.SaveElements(options);
}
private Dictionary<string, string> LoadElements()
{
var fileName = this.FolderPath + this.Name + ".xml";
var list = new Dictionary<string, string>();
if (!File.Exists(fileName))
{
return list;
}
var lastElementName = string.Empty;
using (var reader = new XmlTextReader(fileName))
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
{
lastElementName = reader.Name;
}
break;
case XmlNodeType.Text:
{
list.Add(lastElementName, reader.Value);
}
break;
default:
break;
}
}
}
return list;
}
private void SaveElements(Dictionary<string, string> list)
{
var fileName = this.FolderPath + this.Name + ".xml";
this.DeleteFile(fileName);
var xmlDoc = this.CreateXmlDocument();
this.CreateRootKey(xmlDoc, this.Name, string.Empty);
foreach (var item in list)
{
var xmlElement = xmlDoc.CreateElement(string.Empty, item.Key, string.Empty);
var xmltext = xmlDoc.CreateTextNode(item.Value);
xmlElement.AppendChild(xmltext);
xmlDoc.ChildNodes.Item(1).AppendChild(xmlElement);
}
this.SaveXMLDocument(xmlDoc, fileName);
}
private void CreateRootKey(XmlDocument xmlDoc, string name, string value)
{
var xmlElement = xmlDoc.CreateElement(string.Empty, name, string.Empty);
var xmlText = xmlDoc.CreateTextNode(value);
xmlElement.AppendChild(xmlText);
xmlDoc.AppendChild(xmlElement);
}
private void SaveXMLDocument(XmlDocument document, string fileName)
{
try
{
document.Save(fileName);
}
catch (Exception)
{
}
}
private void DeleteFile(string fileName)
{
if (File.Exists(fileName))
{
try
{
File.Delete(fileName);
}
catch (Exception)
{
}
}
}
private XmlDocument CreateXmlDocument()
{
var xmlDoc = new XmlDocument();
var xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, string.Empty, string.Empty);
xmlDoc.AppendChild(xmlNode);
return xmlDoc;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have over 15 years of development experience, in many different languages, programming styles and platforms. Currently working as a C# coder, and residing in north Herts in the UK. I love lean software development and anything that reduces a grind to leave more time for useful coding!