Reputation: 21
Guys first of all i want to apologize to you about my english. I'm not so good about that. I want to change button and textbox text color and back ground color to using xml.i have xml file like that.It's should work when the program is starting. I hope ı can express myself.
<?xml version=”1.0” encoding=”UTF−8”?>
<components>
<textbox type=” first ”>
<textcolor>BLACK</ textcolor>
<bgcolor>WHITE</bgcolor>
</textbox>
<textbox type=” second ”>
<textcolor>RED</ textcolor>
<bgcolor>WHITE</bgcolor>
</textbox>
<button>
<textcolor>BLUE</ textcolor>
<bgcolor>YELLOW</bgcolor>
</button>
</components>
Upvotes: 2
Views: 1399
Reputation: 319
You can use LINQ-to-XML , so that you can do var doc = XDocument.Load("yourfilepath")
.
For example if your application is in Windows Forms you can use this
var doc = XDocument.Load("settings.xml");
var textboxes = doc.Descendants("textbox");
foreach(var textbox in textboxes)
{
// here you can set as attribute the id of the textbox, i assumed this is it
var attribute = textbox.Attribute("type");
if (attribute == null) throw new Exception("Invalid xml file");
var id = attribute.Value;
if (!Controls.ContainsKey(id)) throw new Exception("Invalid xml file");
var colorNode = textbox.Descendants("textcolor").FirstOrDefault();
if (colorNode == null) throw new Exception("Invalid xml file");
var backgroundColorNode = textbox.Descendants("bgcolor").FirstOrDefault();
if (backgroundColorNode == null) throw new Exception("Invalid xml file");
Controls[id].ForeColor = ColorTranslator.FromHtml(colorNode.Value);
Controls[id].BackColor = ColorTranslator.FromHtml(backgroundColorNode.Value);
}
var button = doc.Descendants("button").FirstOrDefault();
if (button == null) throw new Exception("Invalid xml file");
var colorButton = button.Descendants("textcolor").FirstOrDefault();
var backgroundColorButton = button.Descendants("bgcolor").FirstOrDefault();
//button1 is the id of the button
Controls["button1"].ForeColor = ColorTranslator.FromHtml(colorButton.Value);
Controls["button1"].BackColor = ColorTranslator.FromHtml(backgroundColorButton.Value);
You must add other validations like the color names must be valid if not replace them with a default color or throw an Exception. Also I recommend to use hex format for colors for reliability
Upvotes: 1