[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to search and delete a record in an xml file

Posted on 2016-07-27
3
Medium Priority
?
87 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

873 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