Solved

Help with finding if table in xml file exist using VB.NET

Posted on 2014-03-13
10
253 Views
Last Modified: 2014-03-13
Hi,

My xml files contains multiple tables, example shown below. How do I find if a Table1 ,Table2 or Table3 exist before I search the table?

<Table1>
<Item1_ID><Item1_ID>
<Item1><Item1>
</Table1>
<Table2>
<Item2_ID><Item2_ID>
<Item2><Item2>
</Table2>
<Table3>
<Item3_ID><Item3_ID>
<Item3><Item3>
</Table3>

Thanks

Victor
0
Comment
Question by:vcharles
  • 5
  • 5
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
You could try something like:
        Dim doc As XmlDocument = New XmlDocument()
        doc.LoadXml("<Table1><Item1_ID><Item1_ID><Item1><Item1></Table1><Table2><Item2_ID><Item2_ID><Item2><Item2></Table2><Table3><Item3_ID><Item3_ID><Item3><Item3></Table3>")

        Dim nodes As XmlNodeList = doc.SelectNodes("//*[name()='Table1' or name()='Table2' or name()='Table3'")
        If nodes.Count > 0 Then

            '// found one or more 'Table#' elements

        End If

Open in new window

0
 

Author Comment

by:vcharles
Comment Utility
Hi,

The xml file is loaded in a DataTable. How do I achieve the same from the DataTable?

Thanks,

Victor
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
How are you loading the XML into a DataTable? The XML you posted isn't well formed, so you can't be loading it as is.
0
 

Author Comment

by:vcharles
Comment Utility
I'm using the following code:

Dim curfile As String = (Application.StartupPath + "\LinkTotal.xml")
            fslinkBEL = New System.IO.FileStream(Application.StartupPath + "\LinkTotal.xml", IO.FileMode.Open)
            dtsetlinkBEL.Clear()
            dtsetlinkBEL.ReadXml(fslinkBEL)
            fslinkBEL.Close()

The xml file I send was just to show multiple tables in the xml file.

Victor
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Can you post a bit more of the XML as I cannot load it to test without the top-level elements, or the schema definition.
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!

 

Author Comment

by:vcharles
Comment Utility
Hi,

Below is a better example of the xml file:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <Table1>
    <Link_ID>1</Link_ID>
    <Receiver>BEL</Receiver>
    <Donor>GRC</Donor>
  </Table1>
 <Table1>
    <Link_ID>2</Link_ID>
    <Receiver>LTU</Receiver>
    <Donor>ESP</Donor>
  </Table1>
  <Table2>
    <Link_ID>1</Link_ID>
    <Item_Type>DPOP</Item_Type>
  </Table2>
  <Table2>
    <Link_ID>2</Link_ID>
    <Item_Type>DPEG</Item_Type>
  </Table2>
</DocumentElement>

Thanks,

V.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
Is your XML file loaded into a DataSet rather than a DataTable? If that's the case (which would make more sense), then you can use:
If dtsetlinkBEL.Tables.Contains("Table1") Or dtsetlinkBEL.Tables.Contains("Table2") Or dtsetlinkBEL.Tables.Contains("Table3") Then
    MsgBox("Has a table")
End If

Open in new window

0
 

Author Comment

by:vcharles
Comment Utility
Thanks,


Regarding loading the xml into a DataSet or DataTable, does it impact how fast the results are processed?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
It shouldn't do. If you are loading multiple tables then you need to use a DataSet; you can't load into a DataTable without a schema definition document, which is what was confusing me at the start before realising you must have actually been using a DataSet.
0
 

Author Comment

by:vcharles
Comment Utility
Thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

9 Experts available now in Live!

Get 1:1 Help Now