Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
Step:1
I have a created Textbox,combo box and check box in my form(Dynamically).
Step:2
Insert values into XML like this structure.
XML
<?xml version="1.0"?>
<Root>
  <port0>COM5-Naraayanan-True</port0>
  <port1>COM6-Mohan-False</port1>
</Root>

Here ,COM5 is a Combobox value
Naraayanan is a textbox value
True is a Checkbox value(Checked).
But my Problem is when I read a XML. I got a result except Checkbox.Please tell me how to solve my Problem.
I give my readxml coding
gb is a Group Box in my form .
C#
string sFilename = xmlpath;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(sFilename);
            XmlElement rootElem = xmlDoc.DocumentElement; //Gets the root element, in your xml its "Root"
            for (int i = 0; i < rootElem.ChildNodes.Count ; i++)
            {
                string name = rootElem.ChildNodes[i].Name;
                string value = rootElem.ChildNodes[i].InnerText;
                string[] words = value.Split('-');
                    txt1 = words[0];
                    txt2 = words[1];
                    txt3 = words[2];
                   gb.Controls["Combobox" + (i + 0).ToString()].Text = txt1;
                   gb.Controls["Textbox" + (i + 0).ToString()].Text = txt2;
                   gb.Controls["RB" + (i + 0).ToString()].Text    = txt3;

            }

Last line is problem.Please How to solve my Problem?
Regards,
Lakshmi Narayanan
Posted
Updated 25-May-11 1:50am
v2

I think you should replace the line
C#
gb.Controls["RB" + (i + 0).ToString()].Text    = txt3;

with
C#
CheckBox cb = gb.Controls["RB" + i.ToString()] as CheckBox;
cb.Checked = (txt3=="True");



BTW you don't need txt1,txt2,txt3 variables.
BTW2 your XML is poorly defined: each of the hyphen-separated values should have its own node.
 
Share this answer
 
v3
Comments
naraayanan 25-May-11 8:36am    
Hi,
Thanks for u reply. But i didn't get "Checked"(gb.Controls["RB" + i.ToString()].Checked )in this line.please give any other option.
CPallini 25-May-11 9:21am    
I've updated my answer.
naraayanan 25-May-11 22:41pm    
Hi Pallini,
Thanks for valuable post..... Nice to chat with you and Keep in tough whit me.
Regards,
Lakshmi Narayanan.S
Sergey Alexandrovich Kryukov 26-May-11 13:44pm    
My 5, good catch.
Please see the previous questions -- this is almost a re-post, and the reason is wrong approach; OP is reluctant to take the right route.

Please see my answer and especially my reference to the answers to one of the previous OP's question where good alternatives were already suggested.
--SA
I think your problem is the approach. You're trying to persist UI directly. Instead, you really need to make an intermediate data layer and persist only the data layer.

It looks as extra work but failure to do so will lead you to the dead end in future. By doing what you're doing right now you block your way to upgrade or replace your UI part. Remember, you need to isolate you UI from both the universal and application-specific part of functionality, but you're doing just the opposite.

Now, you don't need to create XML layer at all, as I explained in my solution using Data Contract in my recent answer:
Dynamic creation of elements in XML via C#.net[^].

You need to develop data-to-UI (population) and (UI-to-data) methods which will glue things together.
Use the benefits of loose coupling (http://en.wikipedia.org/wiki/Loose_coupling[^])!

—SA
 
Share this answer
 
C#
public class Properties
{
    private Dictionary<String, String> list;
    private String filename;

    public Properties(String file)
    {
        reload(file);
    }

    public String get(String field, String defValue)
    {
        return (get(field) == null) ? (defValue) : (get(field));
    }
    public String get(String field)
    {
        return (list.ContainsKey(field))?(list[field]):(null);
    }

    public void set(String field, Object value)
    {
        if (!list.ContainsKey(field))
            list.Add(field, value.ToString());
        else
            list[field] = value.ToString();
    }

    public void Save()
    {
        Save(this.filename);
    }

    public void Save(String filename)
    {
        this.filename = filename;

        if (!System.IO.File.Exists(filename))
            System.IO.File.Create(filename);

        System.IO.StreamWriter file = new System.IO.StreamWriter(filename);

        foreach(String prop in list.Keys.ToArray())
            if (!String.IsNullOrWhiteSpace(list[prop]))
                file.WriteLine(prop + "=" + list[prop]);

        file.Close();
    }

    public void reload()
    {
        reload(this.filename);
    }

    public void reload(String filename)
    {
        this.filename = filename;
        list = new Dictionary<String, String>();

        if (System.IO.File.Exists(filename))
            loadFromFile(filename);
        else
            System.IO.File.Create(filename);
    }

    private void loadFromFile(String file)
    {
        foreach (String line in System.IO.File.ReadAllLines(file))
        {
            if ((!String.IsNullOrEmpty(line)) &&
                (!line.StartsWith(";")) &&
                (!line.StartsWith("#")) &&
                (!line.StartsWith("'")) &&
                (line.Contains('=')))
            {
                int index = line.IndexOf('=');
                String key = line.Substring(0, index).Trim();
                String value = line.Substring(index + 1).Trim();

                if ((value.StartsWith("\"") && value.EndsWith("\"")) ||
                    (value.StartsWith("'") && value.EndsWith("'")))
                {
                    value = value.Substring(1, value.Length - 2);
                }

                try
                {
                    //ignore dublicates
                    list.Add(key, value);
                }
                catch { }
            }
        }
    }


}
 
Share this answer
 
You can use LINQ to read xml file.
Try this link:
http://stackoverflow.com/questions/670563/linq-to-read-xml[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900