Solved

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

Posted on 2014-04-29
8
539 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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

624 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