473,609 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ 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 1699
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
3262
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 scripts working on a new server with the most recent version of PHP. I've pretty much taken care of all the various errors that were popping up. Most only pointed out out non-fatal undefined or assumed variables. I've been able to cure most of...
6
1721
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, this is a local page, not one on the Internet). I had it working fine with row-returning SQL, such as SELECT, but was trying to get it to work with non-row-returnign ones as well (such as UPDATE and DELETE). Not only can't I get this bit to work, I...
9
5161
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 is this.... importing a large csv file (189000 lines, 7 fileds per line)
0
1745
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 and the module as well as where I would like the macros to run. Any input on how to make this work would be appreciated. This would automate a former 3 step process into one process.
4
2226
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
1975
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 field. this is wat i've got so far; Private Sub FilmID_BeforeUpdate(Cancel As Integer) If DCount("*", "tblFilmRental", "FilmID=" & Me!) > 0 Then Cancel = True
3
1917
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 as fun as coding it... And yes alarm bells went off when I found myself typing eval(), and I'm sure this is an 'unusual' use of for: else: . And I know it's not very robust. As an ex cuse/planation, this is what happens when you get an idea in...
5
11148
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 statistical data... But I'm just looking to capture the query itself and save it in a logging table. I just need to save an executing query in certain circumstances (if detected an attempted sql injection attack) for logging purposes.
0
8127
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8215
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8398
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5509
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1380
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.