Thursday, March 18, 2010

Delete Selected item from XML Node


All,

Today i gonna share with you code related to XML file.
That is reading and reading XML files.

Following Steps will be shown:
a) First populate listbox with XML data.
b) Second select any item in the Listbox and delete that node from the XML.

Prerequisites:
Add using System.XML namespace above.
Add listbox and a Button(for Delete).
Make an XML file and store in any directory (D directory in my example).

First Below is the Sample XML file :( I have stored it in as : D:\DocumentsOrder.xml).



On Form Load write the below code:

private voidForm1_Load(object sender,EventArgs e)

{

XmlDocument xDoc = newXmlDocument();

xDoc.Load(@"D:\DocumentsOrder.xml");

for (int i = 0; i <>

{

//Add the innertext of the xml document to the listbox

listBox1.Items.AddRange(newobject[] { xDoc.DocumentElement.ChildNodes[i].InnerText });

}

}


On Delete Button Click event write the following Code:


private voidbutton5_Click(object sender,EventArgs e)

{

XmlDocument xDoc = newXmlDocument();

xDoc.Load(@"D:\DocumentsOrder.xml");

//Add the innertext of the xml document to the listbox

xDoc.DocumentElement.RemoveChild(xDoc.DocumentElement.ChildNodes[listBox1.SelectedIndex]);

FileStream WRITER = newFileStream(@"D:\DocumentsOrder.xml", FileMode.Truncate,FileAccess.Write,FileShare.ReadWrite);

xDoc.Save(WRITER);

//Close the writer filestream

WRITER.Close();

//Form1_Load(new object(), new EventArgs());

}



Thats it...Your XML Node(selected item in Listbox) is deleted.

So here is the preview:






Now Go and check your XML...
Hope the selected Node is deleted.

Regards,
Nitin Sharma

Send Outlook E mail using VB macros

All,
Below is the VB Macro code to send mail using Microsoft Outlook :

Lines mark with " ' " are commented.

Sub SendMail()

'

'This macro requires the Outlook Object library to be checked

'in the vba editor Tools > References

Dim bStarted As Boolean

Dim oOutlookApp 'As outloo

Dim oItem 'As Outlook.MailItem

On Error Resume Next

'see if Outlook is running and if so turn your attention there

Set oOutlookApp = GetObject(, "Outlook.Application")

If Err <> 0 Then 'Outlook isn't running

'So fire it up

Set oOutlookApp = CreateObject("Outlook.Application")

bStarted = True

End If

'Open a new e-mail message

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem 'and add the detail to it

.To = "abc@yahoo.com" 'send to this address

.Subject = "This is a Test Mail using VB Macros" 'This is the message subject

.Body = "See attached document" ' This is the message body text

.Attachments.Add ActiveDocument.FullName

ActiveDocument.SaveAs strFilePath & strDate

.Send

'**********************************

'If you want to view the message before it goes

'change the line above from .Send to .Display

'Otherwise the message is sent straight to the Outbox

'and if you have Outlook set to send mail immediately,

'it will simply be Sent

'with no obvious sign that Outlook has operated.

'Apart from the copy in the Outlook Sent folder

'**********************************

End With

If bStarted Then 'If the macro started Outlook, stop it again.

oOutlookApp.Quit

End If

'Clean up

Set oItem = Nothing

Set oOutlookApp = Nothing

End Sub





Regards,

Nitin Sharma