473,805 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Schema Validation in c#

Hi,

I'm really confused as to how to validate XML fragments against a schema in
C#.

I am creating XML through an automated process and have an .xsd which was
given to me to validate against.

Can someone give me some clue as to how to validate my XML string against
this schema document? Is there some great secret that I'm missing?

Thanks,
Dave
Nov 15 '05 #1
3 7421
Look into 'XMLValidatingR eader' in the framework.
SBC

"Dave" <da**@telco.co. uk> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

I'm really confused as to how to validate XML fragments against a schema in C#.

I am creating XML through an automated process and have an .xsd which was
given to me to validate against.

Can someone give me some clue as to how to validate my XML string against
this schema document? Is there some great secret that I'm missing?

Thanks,
Dave

Nov 15 '05 #2
Obviously that's the first place I looked, but I can't make sense of it.
There seem to be a plethora of options for validation and I can't get any to
work.
"Microsoft News" <ch**********@c omcast.net> wrote in message
news:eh******** ******@TK2MSFTN GP10.phx.gbl...
Look into 'XMLValidatingR eader' in the framework.
SBC

"Dave" <da**@telco.co. uk> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

I'm really confused as to how to validate XML fragments against a schema

in
C#.

I am creating XML through an automated process and have an .xsd which was given to me to validate against.

Can someone give me some clue as to how to validate my XML string against this schema document? Is there some great secret that I'm missing?

Thanks,
Dave


Nov 15 '05 #3
The easiest way, in my opinion, is to use the XmlValidatingRe ader class --
specifically, the overload that takes an XML fragment and a parser context.

You need to add the schema to the Schemas and ValidatingType attributes of
the XmlValidatingRe ader before using it to validate the XML.

Here's a code sample which might help:

(Caveat: I set the XmlParserContex t to "null", which may cause issues -- I
haven't actually tested this code. You might want to research the
XmlParserContex t class and supply an instance of this class to the
XmlValidatingRe ader ctor as appropriate.)

Hopefully, this will put you in the right direction!
public class XmlValidator
{
public XmlValidator(st ring xmlFragment, string pathToXsd)
{
XmlValidatingRe ader xmlValidator = new
XmlValidatingRe ader(xmlFragmen t, XmlNodeType.Ele ment, null);
XmlSchemaCollec tion xsdCollection = new
XmlSchemaCollec tion();

if (File.Exists(pa thToXsd))

{
xsdCollection.A dd("urn:my-schema", pathToXsd);
xmlValidator.Sc hemas.Add(xsdCo llection);
}

xmlValidator.Va lidationType = ValidationType. Schema;
vr.ValidationEv entHandler += new
ValidationEvent Handler(MyValid ationHandler);

while (xmlValidator.R ead())
{
// optional element examination here;
// The ValidationEvent event is raised whenever an
error is encountered
}
}

public static void MyValidationHan dler(object sender,
ValidationEvent Args args)
{
// Perform handling for validation error here

}

}
--
----
Doug Erickson [MSFT], Platform SDK UA
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2004 Microsoft Corporation. All rights
reserved.

"Dave" <da**@telco.co. uk> wrote in message
news:eS******** ******@TK2MSFTN GP10.phx.gbl...
Obviously that's the first place I looked, but I can't make sense of it.
There seem to be a plethora of options for validation and I can't get any to work.
"Microsoft News" <ch**********@c omcast.net> wrote in message
news:eh******** ******@TK2MSFTN GP10.phx.gbl...
Look into 'XMLValidatingR eader' in the framework.
SBC

"Dave" <da**@telco.co. uk> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

I'm really confused as to how to validate XML fragments against a
schema
in
C#.

I am creating XML through an automated process and have an .xsd which

was given to me to validate against.

Can someone give me some clue as to how to validate my XML string against this schema document? Is there some great secret that I'm missing?

Thanks,
Dave



Nov 15 '05 #4

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

Similar topics

1
2855
by: Mike | last post by:
Note: My XML experience to date has (unfortunately) been limited to reading and thinking, rather than implementation. Anyway, I am in the process of trying to figure out the most efficient way to validate and transform some very large (potentially over 100MB) XML documents. This is related to another question I will post next, but for now, I want to focus on one particular topic. The data in question has particular business rules...
4
5612
by: joes | last post by:
Hello there I tried for several days to get a simple validation with xml schema & xerces working. Goal for me is tuse JAXP and not specific Xerces classes. I don't get the point what I am doing wrong. Could somebody help me? I didn't find any full example working on the net. Thank you for any hints! If I run the examples below, the parsers parses the file well, no vlaidation is occuring although the schema and xml file does not
2
2154
by: wumingshi | last post by:
Hi, When validating an XML instance, sometimes the schema is not enough to expression the validation rules. Additional validation rules may be expressed in an application-specific way. For example, using XPath or Java method. So is there a concept and/or standard for post-schema validation or application validation? Is there any hook in XML standards that allow me to hook up with my customized validation? Additionally and more...
2
9453
by: Shone | last post by:
I would like to perform a 2-pass XML reading from a stream. Once using the Validating reader, just to confirm the validity against the schema, and next time to do a reading to extract the data. Actually, second time I do a deserialization, the data from XML is fed directly to an object. The problem I am experiencing is the error at the second reading attempt, and error description implies that reader is winded to the end of the XML and...
1
2327
by: Dan Bass | last post by:
There's an XML message I have, that has no namespace information. Then there is a XSD schema that is must validate against, but this has a targetNamespace and xmlns of "http://www.wbf.org/xml/b2mml-v02". How do I get this XML to validate against the Schema in C#? If I use XmlSpy (2005 home edition) to perform the validation, it first inserts namespace and schema information into the XML before validating. Validation then seems to work if...
2
7310
by: Ali | last post by:
I am having problem compiling schema contained in WSDL file when analyzing schema types contained in it (for example http://www.ebout.net/net/GoogleSearch.wsdl). Following code demonstrates my problem: using System.Diagnostics; using System.IO; using System.Xml; using System.Xml.Schema;
1
6412
by: billa1972 | last post by:
Hi, I am trying to hook into Yellow Freight's rating webservice. Below is the wsdl. When i try and create a proxy file with wsdl.exe i get the following errors, see below. Also, when i reference this wsdl in .NET it seems to do it fine, yet there are no objects to reference except RateQuoteBeanService. In the WSDL it looks like there should be getRateQuote, and QUOTEREQUEST, etc.
0
2696
by: Mark Parter | last post by:
I'm trying convert an XML Schema (http://www.elframework.org/projects/xcri/efc_r1.0.xsd/view) to a VB.Net class using the XSD tool provided with the .NET 2 SDK. However, it's failing to generate the class and is outputting the following error(s); Schema validation warning: Undefined complexType 'http://purl.org/dc/elements/1.1/:SimpleLiteral' is used as a base for complex type extension. Line 433, position 5. Schema validation warning:...
2
3047
by: Mark | last post by:
Hi... I've been trying the .Validate() method on the XmlDocument to validate some xml against a schema, but one thing I noted was that unless the document explicitly declares the schema as a default namespace, Validate() just lets it slide through as a success. Is there a way to Validate() either a) mandating against a particular schema, or at the very least b) supplying a default namespace to be used even when not explicitly declared?
9
3023
by: mstilli | last post by:
Hi, I am trying to use schema for server side validation using xerces to catch the validation errors. validating this XML: <Content4> <textarea13></textarea13> <binaryobject3></binaryobject3>
0
9596
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
10604
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
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10103
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
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
7644
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...
1
4316
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
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
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.