Help with checking for existing data element

Posted on 2014-08-04
Last Modified: 2014-08-04

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")
Question by:vcharles
    LVL 62

    Expert Comment

    by:Fernando Soto
    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?

    Author Comment

    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"?>


    LVL 62

    Expert Comment

    by:Fernando Soto
    So the document will only have one Table1 node in it? correct?

    Author Comment

    Yes, one table (Table1) with multiple records.
    LVL 62

    Accepted Solution

    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


    Author Closing Comment

    Thank You.
    LVL 62

    Expert Comment

    by:Fernando Soto
    Not a problem Victor, glad to help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    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…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 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

    16 Experts available now in Live!

    Get 1:1 Help Now