Go Premium for a chance to win a PS4. Enter to Win

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

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

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
vcharles
Asked:
vcharles
  • 7
  • 5
1 Solution
 
Fernando SotoCommented:
Please post sample XML file.
0
 
vcharlesAuthor Commented:
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
 
Fernando SotoCommented:
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
vcharlesAuthor Commented:
Thank you, will get back to you  tomorrow or later tonight.
0
 
vcharlesAuthor Commented:
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
 
vcharlesAuthor Commented:
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
 
Fernando SotoCommented:
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
 
vcharlesAuthor Commented:
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
 
Fernando SotoCommented:
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
 
vcharlesAuthor Commented:
Hi Fernando,
Thank you.
Victor
0
 
vcharlesAuthor Commented:
Thank You!
0
 
Fernando SotoCommented:
Not a problem Victor, as always glad to help.
0

Featured Post

Industry Leaders: 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!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now