Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-29
8
Medium Priority
?
544 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 64

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 64

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

670 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