Solved

Help with checking if table exist in XML file using VB.NET

Posted on 2014-10-31
12
119 Views
Last Modified: 2014-11-05
Hi,

How do check if table exist in XML file using vb.net, I need to check if table exist before executing code to load it in a grid control.

Thanks,

Victor
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40416358
Please post sample XML file.
0
 

Author Comment

by:vcharles
ID: 40416394
Hi Fernando,
Below is an example
<Root>
<tablea>
<Lind_ID>1</Lind_iD>
<itemA>text1</item>
</tablea>
<tableb>
<Lind_ID>1</Lind_iD>
<itemA>text1</item>
</tableb>
<tablec>
<Lind_ID>1</Lind_iD>
<itemA>text1</item>
</tablec>
</Root>
Thanks,
Victor
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40416447
Hi Victor;

Something like this should do what you need.

'' Load the XML into memory, change file path
Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\TableData.xml")

'' Check to see if the XML document have tableX nodes
Dim tableCount As Integer = (From t In xdoc.Root.Descendants() _
                             Where t.Name.ToString().StartsWith("table") _
                             Select t).Count()
                          
If tableCount > 0 Then
   '' The XML file has table data in it
End If   

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vcharles
ID: 40416586
Thank you, will get back to you  tomorrow or later tonight.
0
 

Author Comment

by:vcharles
ID: 40416591
Hi again,
Another quick related question,
How do include tables found in a string variable X and tables not found in another string variable Y ? For example if only tableA and tableB are in the XML file
X  = tableA, tableB
Y  =. tableC
Thanks,
Victor
0
 

Author Comment

by:vcharles
ID: 40417161
Hi Fernando,

Unfortunately not all the tables start with table, sent you an example with table to avoid typing the actual names of the tables but I can see it has an impact on the code, I'm sorry for not being more specific. Below is a better example of the tables:

<Root>
 <AGD>
 <Lind_ID>1</Lind_iD>
 <itemA>text1</item>
 </AGD>
 <NSC>
 <Lind_ID>1</Lind_iD>
 <itemA>text1</item>
 </NSC>
 <FIF>
 <Lind_ID>1</Lind_iD>
 <itemA>text1</item>
 </FIF>
 </Root>

Thanks,

Victor
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40417191
The question is, "checking if table exist in XML file using VB.NET", with your last post how is the program to tell if an XML files have existing Table in them if the nodes may all be different? Is there a way to tell the difference between a table node and a non table node?
0
 

Author Comment

by:vcharles
ID: 40417203
I realized my last question would not make sense unless I put the list of all the tables in a string variable (I.E. Tables = "FIF,NSC,AGD") and somehow check to see if the tables exist against that list.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40417340
Then you can do something like this.

'' List of table names to find within the XML file
Dim tables = New List(of String) ({"FIF", "NSC", "AGD", "XXX"})
'' Load the XML into memory, change file path
Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\TableData.xml")

'' Check to see if the XML document have tables names in the nodes
Dim tableNames = (From t In xdoc.Root.Descendants() _
                  Where tables.Contains(t.Name.ToString()) _
                  Select nodeName = t.Name.ToString()).ToList()

'' The variable tableNames contain a list of all the values
'' in table that are in the XML 

'' The variable notInTableNames is a List of Table names not in the XML file
Dim notInTableNames = tables.Except(tableNames)

'' To test if the XML file contains and of the table names in the XML use the count methon on tableNames.

Open in new window

0
 

Author Comment

by:vcharles
ID: 40417470
Hi Fernando,
Thank you.
Victor
0
 

Author Closing Comment

by:vcharles
ID: 40424042
Thank You!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40424120
Not a problem Victor, as always glad to help.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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