Friday, April 4, 2008

Load user configurations from xml file stored in MySQL

So now I'm gonna show you how I loaded XML saved previously in MySQL. I took the row with the user ID and putted it as
a parameter in my new
LoadUserConfig function. Then I began to study XmlDocument class and found out
that it has wonderful functions .Load(File fileName) which loads XML from a file (which of course wasn't what I really wanted) & another functions .LoadXml(string strSomething) which loads XML directly from string. After that I just used a simple XPath expression to find the nodes that I really wanted and then just use the data from nodes. That's all!

private void LoadUserConfigs(UsersDataSet.T_UsersRow dr)
{
SetFilterCountryCmb(Agences);

XmlDocument xd = new XmlDocument();
xd.LoadXml(dr.MEM_CONFIG);

string xpathExpression = "//fields/field";

XmlNodeList nodes = xd.SelectNodes(xpathExpression);

if (nodes.Count > 0)
{
foreach (XmlNode node in nodes)
{
((ToolStripMenuItem)this.fieldsToolStripMenuItem.DropDown.Items[int.Parse(node.InnerText)]).Checked = false;
//grid.Column(int.Parse(node.InnerText)).Visible = false;

}
}

xpathExpression = "//filters/idCountry";
XmlNode nodeIdCountry = xd.SelectSingleNode(xpathExpression);

xpathExpression = "//filters/idAgency";
XmlNode nodeIdAgency = xd.SelectSingleNode(xpathExpression);

cmbFilterCountry.SelectedValue = int.Parse(nodeIdCountry.InnerText);
cmbFilterAgency.SelectedValue = int.Parse(nodeIdAgency.InnerText);

xpathExpression = "//sorted/key";
XmlNode nodeKey = xd.SelectSingleNode(xpathExpression);
sortedLast = nodeKey.InnerText;


xpathExpression = "//sorted/value";
XmlNode nodeValue = xd.SelectSingleNode(xpathExpression);
sortedCritere = nodeValue.InnerText;

//Console.WriteLine("KEY: " + nodeKey.InnerText + " VALUE: " + nodeValue.InnerText);

if (nodeKey.InnerText != string.Empty || nodeValue.InnerText != string.Empty)
{
SortDataTable(Users, nodeKey.InnerText, nodeValue.InnerText);
}


xpathExpression = "//date/year";
XmlNode nodeYear = xd.SelectSingleNode(xpathExpression);

xpathExpression = "//date/month";
XmlNode nodeMonth = xd.SelectSingleNode(xpathExpression);

CurrentDate = new DateTime(int.Parse(nodeYear.InnerText), int.Parse(nodeMonth.InnerText), 1);


}

No comments: