Solved

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

Posted on 2014-03-13
10
257 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Suggested Solutions

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

772 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