Ok,
Ik heb een klasse geschreven voor het inlezen van Configuraties uit een XML bestand en eventuele aanpassingen ook te bewaren in dit config.xml bestand.
Het config.xml bestand ziet er als volgend uit (geen DTD en XSL gepost):
De Klassedefinitie is als volgend:
Hoe werkt die nu :
de Load() methode leest het volledige XML bestand in en maakt voor elke config node een nieuw configuration object aan dat alle gegevens bewaard.
De Save() methode overloopt all configuration object en veranderd de gegevens in een virtueel XML bestand. Het oude bestand wordt eerst ingeladen, en dan worden de gegevens bewerkt. Daarna wordt het terug bewaard.
Alleen die XmlDocument.Save() onderaan de Save() methode werkt niet.
Ik krijg geen foutmeldingen of crashed.
Ik heb een klasse geschreven voor het inlezen van Configuraties uit een XML bestand en eventuele aanpassingen ook te bewaren in dit config.xml bestand.
Het config.xml bestand ziet er als volgend uit (geen DTD en XSL gepost):
Code:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configurations SYSTEM "config.dtd">
<configurations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="config.xsd">
<config name="Local Connection">
<source>192.168.1.10</source>
<port>1433</port>
<library>DBMSSOCN</library>
<catalog>Neo Phuso</catalog>
</config>
<config name="Remote Connection">
<source>0.0.0.0</source>
<port>1433</port>
<library>DBMSSOCN</library>
<catalog>Neo Phuso</catalog>
</config>
</configurations>
De Klassedefinitie is als volgend:
Code:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Xml;
using System.Windows.Forms;
namespace Stage_Phuso.classes
{
class ConfigurationManager
{
#region Private Datamembers
private string _configurationFile; // Local URI to the config.xml file
private List<Configuration> _configurations; // List of all the configurations
#endregion
#region Constructors
public ConfigurationManager()
{
// Assign a new List to the configurationslist.
this._configurations = new List<Configuration>();
}
public ConfigurationManager(string configurationFile)
{
// Safe the local URI to the config.xml file
this.ConfigurationFile = configurationFile;
// Assign a new List to the configurationslist.
this._configurations = new List<Configuration>();
}
#endregion
#region Public Properties
public string ConfigurationFile
{
get { return this._configurationFile; }
set { this._configurationFile = value; }
}
public List<Configuration> Configurations
{
get { return this._configurations; }
set { this._configurations = value; }
}
#endregion
#region Public Methods
public void Save()
{
// Create a new XmlDocument Object.
XmlDocument doc = new XmlDocument();
// Load the Configuration File into it.
doc.Load(this.ConfigurationFile);
// Now we will modify the data for each Configuration.
for (int i = 0; i < this.Configurations.Count; i++)
{
// We define an XML node which will act as the config node.
XmlNode Node;
// We specify the root of the document.
XmlNode root = doc.DocumentElement;
// Now we select our config node by Using XPath.
Node = doc.SelectSingleNode("//config[@name='" + this.Configurations[i].Name + "']");
// From this config node we collect all the childnodes.
XmlNodeList Childnodes = Node.ChildNodes;
// Now we will loop through the childnodes and update their data.
foreach (XmlNode cNode in Childnodes)
{
if (cNode.NodeType == XmlNodeType.Element)
{
switch (cNode.Name)
{
case "source":
cNode.InnerText = this.Configurations[i].DataSource;
break;
case "port":
cNode.InnerText = this.Configurations[i].Port.ToString();
break;
case "library":
cNode.InnerText = this.Configurations[i].Library;
break;
case "catalog":
cNode.InnerText = this.Configurations[i].Catalog;
break;
}
}
}
// Save the changes to the file.
}
doc.Save(this.ConfigurationFile);
}
public void Load()
{
// Create a new XmlDocument Object.
XmlDocument doc = new XmlDocument();
// Load the Configuration File into it.
doc.Load(this.ConfigurationFile);
// Now we retrieve each config node along with it's childs as a Nodelist.
XmlNodeList nodeList = doc.GetElementsByTagName("config");
// Now we will run over each Node.
for (int i = 0; i < nodeList.Count; i++)
{
// Create a configuration object to store the data.
Configuration myConfig = new Configuration();
// First we need to read the attributevalue of our config node
// and store it as the name of our object.
myConfig.Name = nodeList[i].Attributes[0].Value;
// We store the childs of the current config node in a seperate list.
XmlNodeList childnodes = nodeList[i].ChildNodes;
// Now we can loop through this list.
for (int j = 0; j < childnodes.Count; j++)
{
// We need to check if the node is an actuall element.
if (childnodes[j].NodeType == XmlNodeType.Element)
{
// When we are sure that we are dealing with elements
// we can check their name and retrieve the relevant data from it.
switch (childnodes[j].Name)
{
case "source":
// Store the Datasource.
myConfig.DataSource = childnodes[j].InnerText;
break;
case "port":
// Store the DataPort.
myConfig.Port = int.Parse(childnodes[j].InnerText);
break;
case "library":
// Store the Library Protocol
myConfig.Library = childnodes[j].InnerText;
break;
case "catalog":
// Store the catalog
myConfig.Catalog = childnodes[j].InnerText;
break;
}
}
}
// As last step on the loop we add our object to the list.
this._configurations.Add(myConfig);
}
}
#endregion
}
}
Hoe werkt die nu :
de Load() methode leest het volledige XML bestand in en maakt voor elke config node een nieuw configuration object aan dat alle gegevens bewaard.
De Save() methode overloopt all configuration object en veranderd de gegevens in een virtueel XML bestand. Het oude bestand wordt eerst ingeladen, en dan worden de gegevens bewerkt. Daarna wordt het terug bewaard.
Alleen die XmlDocument.Save() onderaan de Save() methode werkt niet.
Ik krijg geen foutmeldingen of crashed.