Solved

How to search and delete a record in an xml file

Posted on 2016-07-27
3
39 Views
Last Modified: 2016-07-27
Hi Experts,

I have a xml file. I do not want duplicate barcodes. How can I search for a barcode.  Suppose in textbox I entered  'test'. Then It should search the file.  Also how to delete a particular ID. For example- suppose I have the ID- 1, then how can I delete it from the file.

Thanks in advance.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<XMLDataSet xmlns="ALLBARCODES">
  <Barcodes>
    <ID>0</ID>
    <Barcode>test</Barcode>
  </Barcodes>
  <Barcodes>
    <ID>1</ID>
    <Barcode>test1</Barcode>
  </Barcodes>
  <Barcodes>
    <ID>2</ID>
    <Barcode>Test3</Barcode>
  </Barcodes>
</XMLDataSet>
0
Comment
Question by:RadhaKrishnaKiJaya
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41731830
Hi RadhaKrishnaKiJaya;

This should do what you want.
// The XML file to be searched or modified
var filename = "C:/Working Directory/MyData.xml";
XDocument xdoc = XDocument.Load(filename);
XNamespace ns = xdoc.Root.Name.NamespaceName;
// ToLower will make the search case insensitive
var find = textBox1.Text.ToLower();

// Query for a barcode to see if it has duplicates in the file
var results = xdoc.Root.Descendants(ns + "Barcode")
                  .Where(bc => bc.Value.ToLower() == find)
                  .Select(bc => bc.Parent).ToList();
                  
// Query for a ID and delete it from the file
// ID to be deleted
var toDelete = "1";
// This query will throw an exception if more then one ID is found
// So you will need to test for it.
XElement deleteID = xdoc.Root.Descendants(ns + "ID")
                        .Where(d => d.Value == toDelete)
                        .Select(d => d.Parent)
                        .SingleOrDefault();

// This will remove the complete node from the xdoc document
deleteID.Remove();
// You will need to save xdoc to the file system to persist the modification of the document
xdoc.Save(filename);

Open in new window

0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 41731988
Thank you very much. It is working perfectly.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41732016
Not a problem RadhaKrishnaKiJaya; glad I was able to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now