473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

remove item from xml

Hi

I'm using the code below to allow user to browse through some xml document.

System.IO.StreamReader sr = new System.IO.StreamReader(currentPath);

System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);

System.Xml.XmlDocument addresses = new System.Xml.XmlDocument();

addresses.Load(xr);

addressesItems = addresses.SelectNodes("addresses/customer");

tbEntrySought.Text =
addressesItems.Item(NodeNo).SelectSingleNode("uniq ueaddressline").InnerText;

tbMatchingEntry.Text =
addressesItems.Item(NodeNo).SelectSingleNode("addr ess_line1").InnerText +
"\r\n";

User has the ability to click a Delete button to delete a current item form
xml. How can I do that using Item number?

Kind regards

IgorM
Nov 17 '08 #1
3 3737
Hi

I tried this

//XmlNode deleteNode = addresses.SelectSingleNode("addresses/customer");
//addressesItems = addresses.SelectNodes("addresses/customer");

//Create FileStream fs

FileStream fs = new FileStream(currentPath, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
//Create new XmlDocument

System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
//Load the contents of the filestream into the XmlDocument (xmldoc)

xmldoc.Load(fs);
//close the fs filestream

fs.Close();
//Remove the xml node

xmldoc.DocumentElement.RemoveChild(xmldoc.Document Element.ChildNodes[nodeNo]);
// Create the filestream for saving

FileStream WRITER = new FileStream(currentPath, FileMode.Truncate,
FileAccess.Write, FileShare.ReadWrite);
// Save the xmldocument

xmldoc.Save(WRITER);
//Close the writer filestream

WRITER.Close();

But I get exception on line FileStream WRITER = new FileStream(currentPath,
FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
Użytkownik "IgorM" <ig***@live.comnapisał w wiadomo¶ci
news:uP*************@TK2MSFTNGP02.phx.gbl...
Hi

I'm using the code below to allow user to browse through some xml
document.

System.IO.StreamReader sr = new System.IO.StreamReader(currentPath);

System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);

System.Xml.XmlDocument addresses = new System.Xml.XmlDocument();

addresses.Load(xr);

addressesItems = addresses.SelectNodes("addresses/customer");

tbEntrySought.Text =
addressesItems.Item(NodeNo).SelectSingleNode("uniq ueaddressline").InnerText;

tbMatchingEntry.Text =
addressesItems.Item(NodeNo).SelectSingleNode("addr ess_line1").InnerText +
"\r\n";

User has the ability to click a Delete button to delete a current item
form xml. How can I do that using Item number?

Kind regards

IgorM


Nov 17 '08 #2
IgorM wrote:
I tried this

//XmlNode deleteNode = addresses.SelectSingleNode("addresses/customer");
//addressesItems = addresses.SelectNodes("addresses/customer");

//Create FileStream fs

FileStream fs = new FileStream(currentPath, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
//Create new XmlDocument

System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
//Load the contents of the filestream into the XmlDocument (xmldoc)

xmldoc.Load(fs);
//close the fs filestream

fs.Close();
//Remove the xml node

xmldoc.DocumentElement.RemoveChild(xmldoc.Document Element.ChildNodes[nodeNo]);
// Create the filestream for saving

FileStream WRITER = new FileStream(currentPath, FileMode.Truncate,
FileAccess.Write, FileShare.ReadWrite);
// Save the xmldocument

xmldoc.Save(WRITER);
//Close the writer filestream

WRITER.Close();

But I get exception on line FileStream WRITER = new FileStream(currentPath,
FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
Which exception exactly do you get?

On the other hand I am not sure why you deal with those file streams
explicitly, XmlDocument.Load and Save can deal with a file name/path so
doing
XmlDocument doc = new XmlDocument();
doc.Load(currentPath);

doc.DocumentElement.RemoveChild(xmldoc.DocumentEle ment.ChildNodes[nodeNo]);
doc.Save(currentPath);
should suffice.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 17 '08 #3
The error I get is:

IOException was caught
The process cannot access the file 'D:\addressbook.xml' because it is being
used by another process.
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:eg**************@TK2MSFTNGP06.phx.gbl...
IgorM wrote:
>I tried this

//XmlNode deleteNode = addresses.SelectSingleNode("addresses/customer");
//addressesItems = addresses.SelectNodes("addresses/customer");

//Create FileStream fs

FileStream fs = new FileStream(currentPath, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
//Create new XmlDocument

System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
//Load the contents of the filestream into the XmlDocument (xmldoc)

xmldoc.Load(fs);
//close the fs filestream

fs.Close();
//Remove the xml node

xmldoc.DocumentElement.RemoveChild(xmldoc.Documen tElement.ChildNodes[nodeNo]);
// Create the filestream for saving

FileStream WRITER = new FileStream(currentPath, FileMode.Truncate,
FileAccess.Write, FileShare.ReadWrite);
// Save the xmldocument

xmldoc.Save(WRITER);
//Close the writer filestream

WRITER.Close();

But I get exception on line FileStream WRITER = new
FileStream(currentPath, FileMode.Truncate, FileAccess.Write,
FileShare.ReadWrite);

Which exception exactly do you get?

On the other hand I am not sure why you deal with those file streams
explicitly, XmlDocument.Load and Save can deal with a file name/path so
doing
XmlDocument doc = new XmlDocument();
doc.Load(currentPath);

doc.DocumentElement.RemoveChild(xmldoc.DocumentEle ment.ChildNodes[nodeNo]);
doc.Save(currentPath);
should suffice.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: YT | last post by:
Howdy, Why the heck wouldn't this work: for each item in Session.Contents if NOT( Instr( item, "customer_" ) = 1 OR Instr( item, "user_" ) = 1 ) Then Session.Contents( item ) = ""...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
1
by: Que | last post by:
hi i have the following xml document <root> <item> <key> <scode></scode> </key> <scode></scode> <desc></desc> <pc></pc>
7
by: Mike Johnson | last post by:
How do I remove a item that's been selected from a listview? I'm using Visual Basic 2005 express edition. Thanks
2
by: Kela | last post by:
An interesting problem: I have a ListView with LabelEdit set to TRUE. When I change the label, I want to make some decisions as to whether the ListViewItem (that's just been edited) should stay in...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
1
by: dvestal | last post by:
I have a ListView with checkboxes. I want to remove items when the checkboxes are unchecked, but to do so yields an ArgumentOutOfRangeException. Is there an easy way to get around this? A...
4
by: Yansky | last post by:
Got a quick n00b question. What's the difference between del and remove?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.