Solved

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

Posted on 2014-10-31
12
109 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 62

Expert Comment

by:Fernando Soto
Comment Utility
Please post sample XML file.
0
 

Author Comment

by:vcharles
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 

Author Comment

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

Author Comment

by:vcharles
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
Hi Fernando,
Thank you.
Victor
0
 

Author Closing Comment

by:vcharles
Comment Utility
Thank You!
0
 
LVL 62

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio installation 5 32
Where to begin studying? 5 63
Crystal Report for VS2013 17 36
Format column on datatable 7 14
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now