473,513 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB.NET Web Service that validates XML

1 New Member
I am writing a web service that takes in an XML string that will then be validated with an XSD. I need some help, as this is my first web service and first try at validating xml with xsd. Here is my code, with some questions at the end.


Expand|Select|Wrap|Line Numbers
  1. Imports Microsoft.VisualBasic
  2. Imports System.Xml
  3. Imports System.Xml.Schema
  4.  
  5. Public Class DataValidator
  6.     Const SCHEMA_SUFFIX As String = "XMLSchema.xsd"
  7.     Const SCHEMA_NAMESP As String = "activationSchema"
  8.  
  9.     Private Failed As String
  10.  
  11.     Public Function ValidateXML(ByVal xmlData As String, ByVal custId As String) As String
  12.         Failed = ""
  13.         ' Create the XmlSchemaSet class.
  14.         Dim sc As XmlSchemaSet = New XmlSchemaSet()
  15.  
  16.         ' Add the schema to the collection.
  17.         sc.Add(SCHEMA_NAMESP, "http://localhost/ActivationData/" & custId & SCHEMA_SUFFIX)
  18.  
  19.         ' Create and load the XML document
  20.         Dim doc As XmlDocument = New XmlDocument()
  21.         doc.LoadXml(xmlData)
  22.  
  23.         ' Create an XmlNodeReader using the XML document
  24.         Dim nodeReader As XmlNodeReader = New XmlNodeReader(doc)
  25.  
  26.         ' Set the validation settings.
  27.         Dim settings As XmlReaderSettings = New XmlReaderSettings()
  28.         settings.ValidationType = ValidationType.Schema
  29.         settings.ValidationFlags = settings.ValidationFlags _
  30.             Or XmlSchemaValidationFlags.ReportValidationWarnings Or XmlSchemaValidationFlags.AllowXmlAttributes
  31.         settings.Schemas = sc
  32.         AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
  33.  
  34.         ' Create the XmlReader object.
  35.         Dim reader As XmlReader = XmlReader.Create(nodeReader, settings)
  36.  
  37.         ' Parse the file. 
  38.         While reader.Read()
  39.         End While
  40.  
  41.         Return Failed
  42.  
  43.     End Function
  44.  
  45.  
  46.     ' Compile a string of any validation errors
  47.     Private Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs)
  48.         Failed = Failed & "XML Validation Error: {0}" & e.Message & vbCrLf
  49.     End Sub
  50.  
  51. End Class
  52.  
My XSD, which resides at the root of the service:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="C10008XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="activationSchema" elementFormDefault="qualified" targetNamespace="activationSchema">
<xsd:element name="activationData">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="custPO" type="xsd:string" minOccurs="1" />
<xsd:element name="itemCode" type="xsd:string" minOccurs="1" />
<xsd:element name="esn" type="xsd:string" minOccurs="1" />
<xsd:element name="npa" type="xsd:string" minOccurs="1" />
<xsd:element name="mdn" type="xsd:string" minOccurs="1" />
<xsd:element name="msid" type="xsd:string" minOccurs="1" />
<xsd:element name="activationCode" type="xsd:string" minOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

My XML:
<activationData>
<custPO>1421</custPO>
<itemCode>C2125---C1819DGNI-00</itemCode>
<esn>03709911440</esn>
<npa>444</npa>
<mdn>mdn1212</mdn>
<msid>msid1212</msid>
<activationCode>650</activationCode>
</activationData>

When I pass the above xml into the service, I get this error:
"Could not find schema information for the element 'activationData'."

Questions:
1. The element exists in the xml - why the error?
2. In the cs.add statement, how can I get around having to hardcode the URL? Is this how you tell the process where the xsd is?
3. Am I even doing this right? Should I use the nodeReader?

Thank you!
Sep 17 '07 #1
0 1256

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2724
by: Darin | last post by:
I would like to connect to the USPS web service which validates address' and looks up zip codes but the USPS examples are in in Visual Basic 6 while I want to do it in C#. I am reading about...
8
2362
by: Coy | last post by:
I've added the ASPNET user to my local SQL Server 2000, but I still get an unhandled exception: Login failed for user 'C594891-A\ASPNET'. This is when using a web service. A similiar ASP.NET...
3
2374
by: Michel | last post by:
Hi there, I am using a web service in VS2003 (the service itself is a dll - I think it is made in Delphi). When calling it, I get an InvalidOperationException: "There is an error in XML...
13
8443
by: Edje.Rommel | last post by:
Hello, I've have a problem with a webservice. I just want to validate a VAT number by country code and VAT numer. The return value should be like "it's valid" and/or the name where it's...
6
2475
by: yaru22 | last post by:
I'd like to create a program that validates bunch of urls against the w3c markup validator (http://validator.w3.org/) and store the result in a file. Since I don't know network programming, I...
0
972
by: draconas | last post by:
Hi all As part of a project I have a vb program that takes an XML document (payload-XML), which contains a lot of data, but no namespaces. It is run through an XSLT transformation which creates...
0
1026
by: John Wright | last post by:
I have created a web service that will validate a username/password combination against LDAP. I encrypt the username/password, send it to my web service which decrypts the username/password,...
1
1414
by: postmanpat | last post by:
i have to create a login form that validates the users and passwords from a text file. I have another function that can add new users and passwords by writing to a test file split by a delimiter. But...
14
2249
by: Dimon | last post by:
Hi guys, I need an advice ASAP. I have two asp pages with forms but I need to do processing in .NET, i.e. when user submits first form it validates all the fields and if ok data goes to database and...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7162
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7527
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3223
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.