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
Solved

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

Posted on 2014-03-13
10
258 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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