Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-03-13
10
Medium Priority
?
262 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
[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
  • 5
  • 5
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39926267
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
ID: 39926348
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
ID: 39926404
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:vcharles
ID: 39926411
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
ID: 39926440
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
 

Author Comment

by:vcharles
ID: 39926456
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 2000 total points
ID: 39926492
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
ID: 39926565
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
ID: 39926588
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
ID: 39926617
Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 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