Solved

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

Posted on 2014-10-31
12
115 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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