Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Help with checking for existing data element

Hi,

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")
0
vcharles
Asked:
vcharles
  • 4
  • 3
1 Solution
 
Fernando SotoCommented:
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?
0
 
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"?>
<Root>
  <Table1>
    <SN>9089</SN>
    <AGD>BHJK</AGD>
    <CHPT>104</CHPT>
    <RIC>YUXRT</RIC>
  </Table1>
</Root>

Thanks,

victor
0
 
Fernando SotoCommented:
So the document will only have one Table1 node in it? correct?
0
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.

 
vcharlesAuthor Commented:
Yes, one table (Table1) with multiple records.
0
 
Fernando SotoCommented:
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
    xdoc.Root.Element("Table1").Add(<CA></CA>)
End If

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

Open in new window

0
 
vcharlesAuthor Commented:
Thank You.
0
 
Fernando SotoCommented:
Not a problem Victor, glad to help.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now