473,322 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Simple Xml Validation with strings

Hello,

I have two strings;

string XmlFragment; //contains the xml document
string XsdSchema; //contains the xsd schema

I would like to validate the XmlFragment against the XsdSchema. What is
a clean, simple way for accomplishing this?

Bye,

Ward


Nov 12 '05 #1
2 1375

Try something like this:

1) Load your XsdSchema into a StreamReader
MemoryStream ms = new MemoryStream((new
Text.ASCIIEncoding()).GetBytes(XsdSchema));
StreamReader schemaReader = new StreamReader(ms);

2) Validate your XmlFragment on the StreamReader
XmlTextReader xmlReader = new XmlTextReader(new
System.IO.StringReader(XmlFragment));
XmlValidatingReader reader = new XmlValidatingReader(xmlReader);
reader.ValidationType = ValidationType.Schema;
reader.Schemas.Add (XmlSchema.Read(schemaReader, null));

while (reader.Read())
{}
reader.Close();
xmlReader.Close();

I hope it could help.

Lionel.

"Ward Bekker" <wa*********@replacethisEquanimity.nl> a écrit dans le message
de news: Oq****************@TK2MSFTNGP10.phx.gbl...
Hello,

I have two strings;

string XmlFragment; //contains the xml document
string XsdSchema; //contains the xsd schema

I would like to validate the XmlFragment against the XsdSchema. What is a
clean, simple way for accomplishing this?

Bye,

Ward


Nov 12 '05 #2
Lionel LASKE wrote:
Try something like this:

1) Load your XsdSchema into a StreamReader
MemoryStream ms = new MemoryStream((new
Text.ASCIIEncoding()).GetBytes(XsdSchema));
StreamReader schemaReader = new StreamReader(ms);

2) Validate your XmlFragment on the StreamReader
XmlTextReader xmlReader = new XmlTextReader(new
System.IO.StringReader(XmlFragment));
XmlValidatingReader reader = new XmlValidatingReader(xmlReader);
reader.ValidationType = ValidationType.Schema;
reader.Schemas.Add (XmlSchema.Read(schemaReader, null));

while (reader.Read())
{}
reader.Close();
xmlReader.Close();

I hope it could help.

Lionel.

"Ward Bekker" <wa*********@replacethisEquanimity.nl> a écrit dans le message
de news: Oq****************@TK2MSFTNGP10.phx.gbl...
Hello,

I have two strings;

string XmlFragment; //contains the xml document
string XsdSchema; //contains the xsd schema

I would like to validate the XmlFragment against the XsdSchema. What is a
clean, simple way for accomplishing this?

Bye,

Ward


Hi Lionel,

Thank you very much for the example. I'll try to implement it.

Bye,

Ward
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: greg.scharlemann | last post by:
I've been playing with this form validation method for a while and have tried an array of things but haven't had any luck with a couple items. 1. The validateForm() function doesn't detect when...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
12
by: Martin Drautzburg | last post by:
I would like to validate sql strings, which are spread all over the code, i.e. I run ("prepare") them against a database to see if it happy with the statements. Spelling errors in sql have been a...
8
by: Bryan | last post by:
I want my business objects to be able to do this: class Person(base): def __init__(self): self.name = None @base.validator def validate_name(self): if not self.name: return
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.