Help with checking for existing data element


How do you check if data element exist before including in xml file to avoid duplicate data elements?  

 Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\FILE1.xml")
        Dim results = (From n In xdoc.Root.Descendants("AOP6B")
                       Select n).ToList()
        results.ForEach(Sub(n) n.Add(<CA></CA>))
        xdoc.Save(Application.StartupPath + "\FILE1.xml")
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Victor;

Please supply a true representation of the XML file.
When you say,, "checking for existing data element", is this within one parent node or in all nodes?
Does this mean that you want to insert the an empty <CA></CA> node in the document?
Where in the document do you want to insert the node?
vcharlesAuthor Commented:
Hi Fernando,

Below is an example of the xml file. I would like to add <CA></CA> at the end of the table if it does not already exist.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>


Fernando SotoRetiredCommented:
So the document will only have one Table1 node in it? correct?
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

vcharlesAuthor Commented:
Yes, one table (Table1) with multiple records.
Fernando SotoRetiredCommented:
Hi Victor;

Try it like this.

'' Load the XML document
Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\FILE1.xml")

'' Get a count of the number of CA nodes in the only Table1 node
Dim results = (From n In xdoc.Root.Descendants("CA")
               Select n).Count()

'' If count is zero it was not found so you can add it
If results = 0 then
End If

'' Save the document
xdoc.Save(Application.StartupPath + "\FILE1.xml")

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You.
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.