473,387 Members | 1,572 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,387 software developers and data experts.

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 7399
Look into 'XMLValidatingReader' in the framework.
SBC

"Dave" <da**@telco.co.uk> wrote in message
news:%2****************@TK2MSFTNGP10.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**********@comcast.net> wrote in message
news:eh**************@TK2MSFTNGP10.phx.gbl...
Look into 'XMLValidatingReader' in the framework.
SBC

"Dave" <da**@telco.co.uk> wrote in message
news:%2****************@TK2MSFTNGP10.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 XmlValidatingReader 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 XmlValidatingReader before using it to validate the XML.

Here's a code sample which might help:

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

Hopefully, this will put you in the right direction!
public class XmlValidator
{
public XmlValidator(string xmlFragment, string pathToXsd)
{
XmlValidatingReader xmlValidator = new
XmlValidatingReader(xmlFragment, XmlNodeType.Element, null);
XmlSchemaCollection xsdCollection = new
XmlSchemaCollection();

if (File.Exists(pathToXsd))

{
xsdCollection.Add("urn:my-schema", pathToXsd);
xmlValidator.Schemas.Add(xsdCollection);
}

xmlValidator.ValidationType = ValidationType.Schema;
vr.ValidationEventHandler += new
ValidationEventHandler(MyValidationHandler);

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

public static void MyValidationHandler(object sender,
ValidationEventArgs 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**************@TK2MSFTNGP10.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**********@comcast.net> wrote in message
news:eh**************@TK2MSFTNGP10.phx.gbl...
Look into 'XMLValidatingReader' in the framework.
SBC

"Dave" <da**@telco.co.uk> wrote in message
news:%2****************@TK2MSFTNGP10.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
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...
4
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...
2
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...
2
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....
1
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...
2
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...
1
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...
0
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...
2
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...
9
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>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...

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.