remove item from xml

I

IgorM

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("uniqueaddressline").InnerText;

tbMatchingEntry.Text =
addressesItems.Item(NodeNo).SelectSingleNode("address_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
 
I

IgorM

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.DocumentElement.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);
 
M

Martin Honnen

IgorM said:
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.DocumentElement.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.DocumentElement.ChildNodes[nodeNo]);
doc.Save(currentPath);
should suffice.
 
I

IgorM

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 said:
IgorM said:
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.DocumentElement.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.DocumentElement.ChildNodes[nodeNo]);
doc.Save(currentPath);
should suffice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top