473,486 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help on Save/Update/Delete XML

1 New Member
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <root>
  3.   <groups>
  4.     <group gid="1" name="CSM">
  5.       <contact cid="1" />
  6.       <contact cid="2" />
  7.       <contact cid="3" />
  8.       <contact cid="4" />
  9.     </group>
  10.     <group gid="2" name="RISIK">
  11.       <contact cid="5" />
  12.       <contact cid="6" />
  13.       <contact cid="7" />
  14.       <contact cid="8" />
  15.     </group>
  16.   </groups>
  17.   <contacts>
  18.     <contact cid="1" name="Ahmad" phoneno="0133881830" email="a@a.com" />
  19.     <contact cid="2" name="Nassem" phoneno="0123881830" email="a@a.com" />
  20.     <contact cid="3" name="Kuryu" phoneno="0164524122" email="a@wer.com" />
  21.     <contact cid="4" name="Moi" phoneno="0133881830" email="a@ter.com" />
  22.     <contact cid="5" name="Asrul" phoneno="0133511830" email="a@ter.com" />
  23.     <contact cid="6" name="Aizal" phoneno="0133971830" email="a@ter.com" />
  24.     <contact cid="7" name="Kak Esma" phoneno="0132069600" email="a@ter.com" />
  25.     <contact cid="8" name="Amse" phoneno="0132069600" email="a@ter.com" />
  26.   </contacts>
  27. </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
  1. private void savecontactingrouping(string gid) //save contact in group
  2.         {
  3.             XmlDocument xmlfonbuk = new XmlDocument();
  4.             xmlfonbuk.Load("../../fonbuk.dml");
  5.  
  6.             XmlNodeList groupnode = xmlfonbuk.SelectNodes("/root/groups/group[@gid=" + gid + "]");
  7.  
  8.             foreach (XmlNode childnode in groupnode)
  9.             {
  10.                 childnode.ParentNode.RemoveAll();
  11.             }
  12.  
  13.  
  14.             XmlElement Elm = xmlfonbuk.CreateElement("contact");
  15.             // Create its attributes
  16.             foreach (string Cid in grouplistbox1.Items)
  17.             {
  18.                 Elm.SetAttribute("cid", Cid);
  19.                 xmlfonbuk.SelectSingleNode("/root/groups/group[@gid=" + gid + "]").AppendChild(Elm);
  20.                 xmlfonbuk.Save("../../fonbuk.dml");
  21.             }
  22.         }        
  23.  
Expand|Select|Wrap|Line Numbers
  1. private void editgroupdetail(string name, int gid) //edit group detail
  2.         {
  3.             XmlDocument xmlfonbuk = new XmlDocument();
  4.             xmlfonbuk.Load("../../fonbuk.dml");
  5.  
  6.             XmlElement Elm = xmlfonbuk.CreateElement("group");
  7.  
  8.             // Create its attributes
  9.             Elm.SetAttribute("gid", Convert.ToString(gid));
  10.             Elm.SetAttribute("name", name);
  11.  
  12.             XmlNode oldnode = xmlfonbuk.SelectSingleNode("/root/groups/group[@gid = " + gid + "]");
  13.  
  14.             oldnode.ParentNode.ReplaceChild(Elm, oldnode);
  15.             xmlfonbuk.Save("../../fonbuk.dml");
  16.  
  17.         }
  18.  
Feb 20 '08 #1
1 1695
dorinbogdan
839 Recognized Expert Contributor
Hi,

You don't need to replace any node when update.
To edit the "name" attribute of a "group" node:
Expand|Select|Wrap|Line Numbers
  1. XmlNode oldnode = xmlfonbuk.SelectSingleNode("/root/groups/group[@gid = " + gid + "]");
  2. oldnode.Attributes.GetNamedItem("name").Text = "CXX";
  3.  
To add a "contact" node it should work if using CreateElement() and AppendChild().

Regards,
Dorin.
Feb 22 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3252
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old...
6
1712
by: Alan Silver | last post by:
Hello, I have an ASP that takes a connection string and SQL statement in the querystring and is supposed to return the XML representation of the recordset to the Response stream (don't worry,...
9
5149
by: tym | last post by:
HELP!!! I'm going round the twist with this... I have a VB6 application which is using DAO to access a database (Please - no lectures on ADO, I know what I'm doing with DAO!!) Ok, problem...
0
1736
by: jpodesta | last post by:
Hello- I am fairly new to MS Access and would like to use some macros in .xls in an Access Module. I have tried to do this on my own but failed to make it work. I have included the xls macros...
4
2202
by: jpodesta | last post by:
I would like to add the xls macros below to the module listed. I am looking for advice as to the best way to go about this. module Public Function clean_pn(pn_in As Variant) As String
8
1966
by: DP | last post by:
hi, i've got a price of code, which checks to see if a film is on rent, or available. but how can i actualyl make the cancel button do somthing? because, the cursor gets stuck on the filmID...
3
1910
by: Matthew Warren | last post by:
I have the following piece of code, taken from a bigger module, that even as I was writing I _knew_ there were better ways of doing it, using a parser or somesuch at least, but learning how wasn't...
5
11137
by: =?ISO-8859-1?Q?Gear=F3id?= | last post by:
Hey, This may sound odd, but is there anyway to catch the current or just run query from inside a trigger? Kinda like how profiler displays the query just as you've run it, along with all the...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.