Solved

Help with checking if table exist in xml file using vb.net

Posted on 2014-04-29
8
538 Views
Last Modified: 2014-04-30
Hi,

How do you check if a table exist in an xml file using VB.NET?

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
  • 4
  • 4
8 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40031977
Hi Victor;

Can you be a little bit more specific. What do you mean by a table in an XML file?
0
 

Author Comment

by:vcharles
ID: 40032011
Hi,

I mean if in my xml files I have the following"
<Root>
<Table1>
Data Elements
</Table1>
Data Elements
<Table2>
Data Elements
</Table2>
<Table3>
Data Elements
</Table3>
<Table4>
Data Elements
</Table4>
</Root>

I would like to check to see if the tables exist in the xml file.

Thanks,

Victor
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40032142
Hi Victor;

To validate a XML document in the manner you are looking for would be to use a XSD file. Below is sample code of how that can be done.

Imports System.Xml.Schema

'' A class level variable
Dim errors As Boolean = False 

'' Load the XSD document into memory
Dim xsdMarkup As XDocument = XDocument.Load("C:\Working Directory\linkfile1.xsd")
'' Create an XmlSchemaSet to help in validating the XML document 
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", xsdMarkup.CreateReader)
'' Load the Document to be validated
Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\linkfile1.xml")
Console.WriteLine("Validating doc1")
errors = False
'' Validate the XML document to the XSD file
xdoc.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("xdoc {0}", If(errors, "did not validate", "validated"))

Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
    Console.WriteLine("{0}", e.Message)                                 
    errors = True                                                       
End Sub

Open in new window


The XSD document that was used to validate the XML document
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Root" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Root" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="1" maxOccurs="unbounded">
        <xs:element name="Table1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ID" type="xs:string" minOccurs="0" />
              <xs:element name="ctry" type="xs:string" minOccurs="0" />
              <xs:element name="Item" type="xs:string" minOccurs="0" />
              <xs:element name="Loc" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Table2">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ID" type="xs:string" minOccurs="0" />
              <xs:element name="NSS" type="xs:string" minOccurs="0" />
              <xs:element name="NASC" type="xs:string" minOccurs="0" />
              <xs:element name="FIF" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Table3">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ID" type="xs:string" minOccurs="0" />
              <xs:element name="EMD" type="xs:string" minOccurs="0" />
              <xs:element name="CUN" type="xs:string" minOccurs="0" />
              <xs:element name="NMN" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

Open in new window

0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

Author Comment

by:vcharles
ID: 40032172
Hi,

Thanks for the code to validate the xml file. How do you create an .xsd for existing an existing xml file and for an xml created by code? I'm trying to locate an old post with the solution but so far no luck.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40032398
Hi;

Here is a link to Microsoft documentation, How to: Create an XML Schema Set by Using the XML to Schema Wizard, this will infer the XSD from a XML document. You may need to modify the XSD to meet your needs.
0
 

Author Comment

by:vcharles
ID: 40032449
Thank You.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40032456
Not a problem Victor, glad to help.
0
 

Author Closing Comment

by:vcharles
ID: 40033828
Thank You.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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