Expand|Select|Wrap|Line Numbers
- <?xml version="1.0" encoding="utf-8"?>
- <root>
- <groups>
- <group gid="1" name="CSM">
- <contact cid="1" />
- <contact cid="2" />
- <contact cid="3" />
- <contact cid="4" />
- </group>
- <group gid="2" name="RISIK">
- <contact cid="5" />
- <contact cid="6" />
- <contact cid="7" />
- <contact cid="8" />
- </group>
- </groups>
- <contacts>
- <contact cid="1" name="Ahmad" phoneno="0133881830" email="a@a.com" />
- <contact cid="2" name="Nassem" phoneno="0123881830" email="a@a.com" />
- <contact cid="3" name="Kuryu" phoneno="0164524122" email="a@wer.com" />
- <contact cid="4" name="Moi" phoneno="0133881830" email="a@ter.com" />
- <contact cid="5" name="Asrul" phoneno="0133511830" email="a@ter.com" />
- <contact cid="6" name="Aizal" phoneno="0133971830" email="a@ter.com" />
- <contact cid="7" name="Kak Esma" phoneno="0132069600" email="a@ter.com" />
- <contact cid="8" name="Amse" phoneno="0132069600" email="a@ter.com" />
- </contacts>
- </root>
Please view image above and explanation below to know what I am doing
Details on application: Gsm modem application developed using Visual C# 2005 Express Edition. A winform application using C#.
In my application, the group resides in a listbox. There is also a 2 other listbox that contains Member and Non-Member respectively.
When a user click on a group for example, the member listbox will be populated with the contacts. To add members in a group, we click "<" button and the member listbox will be populated with a new name from the non-member listbox. To remove member we click on the ">" button.
We can also update the group name, by selecting the group listbox and click on the edit button. The group name will appear in the Group Name textbox. We change the name and click save.
Question:-
- How do I update group e.g. CSM to CXX without updating / replacing the child under it. Currently when I update the parent, the child under it vanishes or overwritten with the new entry.
- How do I add the contact in a specified group?
P/S: Probably both (Q1) and (Q2) can be summed up into one method. This is because when we update a group, the contact below it will need to be populated.
Below is the code what I have attempted. Thank you in advance.
Expand|Select|Wrap|Line Numbers
- private void savecontactingrouping(string gid) //save contact in group
- {
- XmlDocument xmlfonbuk = new XmlDocument();
- xmlfonbuk.Load("../../fonbuk.dml");
- XmlNodeList groupnode = xmlfonbuk.SelectNodes("/root/groups/group[@gid=" + gid + "]");
- foreach (XmlNode childnode in groupnode)
- {
- childnode.ParentNode.RemoveAll();
- }
- XmlElement Elm = xmlfonbuk.CreateElement("contact");
- // Create its attributes
- foreach (string Cid in grouplistbox1.Items)
- {
- Elm.SetAttribute("cid", Cid);
- xmlfonbuk.SelectSingleNode("/root/groups/group[@gid=" + gid + "]").AppendChild(Elm);
- xmlfonbuk.Save("../../fonbuk.dml");
- }
- }
Expand|Select|Wrap|Line Numbers
- private void editgroupdetail(string name, int gid) //edit group detail
- {
- XmlDocument xmlfonbuk = new XmlDocument();
- xmlfonbuk.Load("../../fonbuk.dml");
- XmlElement Elm = xmlfonbuk.CreateElement("group");
- // Create its attributes
- Elm.SetAttribute("gid", Convert.ToString(gid));
- Elm.SetAttribute("name", name);
- XmlNode oldnode = xmlfonbuk.SelectSingleNode("/root/groups/group[@gid = " + gid + "]");
- oldnode.ParentNode.ReplaceChild(Elm, oldnode);
- xmlfonbuk.Save("../../fonbuk.dml");
- }