Solved

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

Posted on 2014-04-29
8
520 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
  • 4
  • 4
8 Comments
 
LVL 62

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 62

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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 62

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 62

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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 video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

22 Experts available now in Live!

Get 1:1 Help Now