Solved

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

Posted on 2014-03-13
10
254 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
 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

16 Experts available now in Live!

Get 1:1 Help Now