473,624 Members | 2,278 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="C10008XMLSc hema"
xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns="activati onSchema" elementFormDefa ult="qualified" targetNamespace ="activationSch ema">
<xsd:element name="activatio nData">
<xsd:complexTyp e>
<xsd:sequence >
<xsd:element name="custPO" type="xsd:strin g" minOccurs="1" />
<xsd:element name="itemCode" type="xsd:strin g" minOccurs="1" />
<xsd:element name="esn" type="xsd:strin g" minOccurs="1" />
<xsd:element name="npa" type="xsd:strin g" minOccurs="1" />
<xsd:element name="mdn" type="xsd:strin g" minOccurs="1" />
<xsd:element name="msid" type="xsd:strin g" minOccurs="1" />
<xsd:element name="activatio nCode" type="xsd:strin g" minOccurs="1" />
</xsd:sequence>
</xsd:complexType >
</xsd:element>
</xsd:schema>

My XML:
<activationData >
<custPO>1421</custPO>
<itemCode>C21 25---C1819DGNI-00</itemCode>
<esn>0370991144 0</esn>
<npa>444</npa>
<mdn>mdn1212</mdn>
<msid>msid121 2</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 1259

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

Similar topics

2
2740
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 web services but am lost. Can someone share some guidance if they have been able to accomplish this? Thanks
8
2363
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 application works perfectly fine, even before I gave any access to the ASPNET account. I read KB article 316989. In SQL Server 2000, I've added a login, dbaccess, and granted full rights to the 'C594891-A\ASPNET user. This doesn't make any...
3
2376
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 document (2, 5587)", with an inner exception: "The referenced element with ID '2' was not found in the document." I use SOAP toolkit to trace the messages. Below are the return message and the schema (shortened for clarity). They look fine to me...
13
8455
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 registered to. To do this i can access the webservice on the following location: http://ec.europa.eu/taxation_customs/vies/api/checkVatPort A WSDL file is also available:
6
2479
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 have no idea how to start coding this program. I was looking at the python library and thought urllib or urllib2 may be used to make this program work.
0
977
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 an XML message (Message XML) to be dilivered to another system, then that XML message is validated in code. The Message XML to the other system does use namespaces and XSD validation, and one of its XML fields is the original XML document, in it's...
0
1029
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, validates the user against LDAP and returns some properties about the user to the client. When I test the application in Visual Studio the client runs just fine. When I deploy the service to my Local IIS, none of the methods work. I suspect it has...
1
1418
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 i dont know how to read from the text file and validates one user one by one when i key in in the login form. the example format of the text file: peter|123 jane|789 hunter|007...left is the username and password is on the right. i dont need sql...
14
2255
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 then it redirects user to second page with some fields pre-popped. So I decided to create a web service which does everything I need with all form field values from first form but the problem is I'm not sure how to redirect user to the next form...
0
8177
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7170
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
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 we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.