Solved

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

Posted on 2014-10-31
12
114 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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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