Solved

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

Posted on 2014-04-29
8
536 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 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS 2017 18 101
C# Service FileSystemwatcher isse 9 46
Asp.Net Session Question 2 33
Using/Import statement in code doesn't see reference of dll just added. 3 25
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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