Validating XML Files Against XSD Schemas in .NET 1.x and 2.0
page 4 of 6
by Keyvan Nayyeri
Feedback
Average Rating: 
Views (Total / Last 10 Days): 78724/ 792

More about ValidationEventArgs

Your ValidationEventArgs parameter in ValidationEventHandler has some useful properties.

·         Exception: It is an instance of XmlSchemaException object.  This object has several properties that can help you to find more information about the error that has been found in your XML file structure during the validation.

·         Message: The string value of error or warning that has been found in your XML file in current validation iteration.

·         Severity: This is an XmlSeverityType enumeration value and can help you to specify that your validation process has faced an error or warning.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 7 and 7 and type the answer here:

User Comments

Title: Nice article   
Name: Vijay Jadhav
Date: 5/6/2009 10:14:15 AM
Comment:
Thanks for your code. This works in Gr8 manner. Thanks for a such article.
Title: Just right   
Name: Andy
Date: 5/1/2009 1:51:45 PM
Comment:
Thankyou, this is clearer than the Microsoft documentation on XML validation. You've saved me a lot of time!
Title: Good idea   
Name: Kristoffer Sjöwall
Date: 1/30/2009 5:23:24 AM
Comment:
It's probably a good idea to close the StreamReader object (SR) aswell as the XmlTextReader object (Reader) at end.
Title: please help me   
Name: T V N
Date: 12/13/2008 3:17:03 AM
Comment:
public static string valid1(XmlDocument xmlDoc, XmlTextReader xsdDoc)
{
validationMessage = string.Empty;
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add(string.Empty, xsdDoc);
XmlTextReader reader = new XmlTextReader(new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xmlDoc.OuterXml)));
XmlValidatingReader validator = new XmlValidatingReader(reader);
validator.Schemas.Add(xsc);
validator.ValidationEventHandler += new ValidationEventHandler(Functionerr);
while (validator.Read()) {}
validator.Close();
return validationMessage;
}
public static void Functionerr(object sender, ValidationEventArgs args)
{
validationMessage = args.Message;
}
Some PC run ok, but some PC not run and validationMessage return 1.
can I help me.
thanks a lot
Title: xml   
Name: Raghu
Date: 11/14/2008 8:59:05 AM
Comment:
need to validate xml with xsd
Title: Senior Software Consultant   
Name: Jeff Huckins
Date: 8/22/2008 11:04:44 AM
Comment:
Just like somebody else that I see posted a message on this article, I have implemented the .NET 2.0 solution and it doesn't find any errors when I inject them. Can somebody tell me what is wrong here? (jhuckins@zaio.com)

Thanks in advance
Jeff
Title: Thanks Sandeep Meravi, Meghna Godhani   
Name: dev
Date: 7/7/2008 8:21:56 AM
Comment:
your code has helped me a lot, thanks a lot for this code
:)
Title: AVP   
Name: Prem
Date: 6/7/2008 8:59:19 AM
Comment:
Excellent article.
Title: Validate xml with xsd   
Name: Virus
Date: 6/3/2008 12:07:38 PM
Comment:
Its too good and working fine.
Title: Validating XML Files Against XSD Schemas in .NET 2.0   
Name: Sandeep Meravi, Meghna Godhani
Date: 2/20/2008 2:23:23 AM
Comment:
Here we are posting the most precise code to validate to an XML File using schema .

private void ValidatingXML()
{
try
{
string xmlFile = Server.MapPath("Dvdlist.xml");
string xsdFile = Server.MapPath("DVDList.xsd");


XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;

settings.Schemas.Add("", xsdFile);


XmlReader objXmlReader = XmlReader.Create(xmlFile, settings);

while (objXmlReader.Read())
{ }
}
catch (XmlSchemaValidationException e)
{
Response.Write(e.Message.ToString());
}


}
Title: How is this called?   
Name: Kevin
Date: 2/14/2008 10:41:32 PM
Comment:
Somewhat new, but I placed the *.cs file in, do a using and calling it the way I think it should be called but I keep getting the following error:

Value cannot be null.
Parameter name: schema

Can someone help me out? I'm sure I'm doing something blatantly wrong here.
Title: Developer   
Name: Lakshmi
Date: 2/7/2008 5:32:02 AM
Comment:
very nice example and presented in a good way,explaining each and every step
Thank u
Title: how to validate xmlstring or xmlnode in string format   
Name: Anki
Date: 12/5/2007 4:51:16 AM
Comment:
can anybody tell me how to validate a single xmlnode against xmlschema or nodes in string format? this example only validates a xmlfile.
Title: Validating XML Files Against XSD Schemas in .NET 1.x and 2.0   
Name: john
Date: 8/21/2007 8:11:45 PM
Comment:
I tested the code for .net 2.0, and it did not catch any of the errors i tried including:
1. node names not in the schema
2. node values that were not in an enumerated list in the schema. What does it catch?
Title: Details: Problem with XmlSchema.Read.....please help   
Name: Hemendra Vyas
Date: 8/17/2007 7:41:29 AM
Comment:
I thnk some problem came in the last post so pasting it again:

This is a part of code that inserts a sub-node inside a node.If i try for the first time after rebuilding the code this error is received. If i try again, I won't receive this message.


The Inner message is :
Could not find file"StructureLinkage.xsd"

Details
Message:
Failure to ad schema to enable nodal linkage validation-check xsd location.
Could not find file"StructureLinkage.xsd"

If after clicking on "OK" to the current message I again try it, then in the below code then the value of "schema_collection_object" in not null and is of type XMLSchemaCollection.

Please help.
Title: Problem with XmlSchema.Read.....please help   
Name: Hemendra Vyas
Date: 8/17/2007 3:29:10 AM
Comment:
Please help out in following code snippet, please reply me at hvyas@irevna.com:

For the first time it gives prompt while XmlSchema.Read(). Second time schema_collection_object is not null and so does not enter into this block of code.

Please help............



private static XmlSchemaCollection schema_collection_object;
private static XmlSchema m_ifschema;
-------
-------
-------

if (schema_collection_object==null)
{
schema_collection_object=new XmlSchemaCollection();

XmlTextReader xmlFile=new XmlTextReader(path);
//path contains the path of the .xsd file

m_ifschema=XmlSchema.Read(xmlFile,new ValidationEventHandler(ValidationCallBack));

//*******error is genarated in the above statement



m_ifSchema.Compile(newValidationEventHandler(Valid ationCallBack));

schema_collection_object.Add(m_ifSchema);
}
Title: XMl Validation   
Name: Anil Dudhani
Date: 8/17/2007 1:54:52 AM
Comment:
In some cases this validation is failed..see example send me message...
Title: Excellent Working Example   
Name: Rob
Date: 6/14/2007 10:12:18 AM
Comment:
This is the first example I've seen that both works and has a simple interface. Thanks for taking the time to write an excellent article and including a simple class that can be used very easily in other projects.
Title: Developer   
Name: sudhakar
Date: 6/7/2007 9:17:36 AM
Comment:
I am working with .Net 2.0. i am providing with the required xml string and xml schema string on which the xml string is to be validated. My code is as follows:


while (objXmlReader.Read())
{}
DataSet objDataSet=new DataSet();
objDataSet.ReadXml(Reader, XmlReadMode.Auto);


I find the data is not loaded into the dataset.. An empty dataset is returned without any exception.It was an urgent issue for me. Please help me in this regards. Thanks in advance
Title: { programmer }   
Name: selva
Date: 5/17/2007 9:00:19 AM
Comment:
Nice coding
Title: Developer   
Name: John (London)
Date: 4/15/2007 11:38:59 AM
Comment:
Many thanks for taking the time to share this - much appreciated
Title: Developer   
Name: Ramesh
Date: 4/12/2007 1:46:39 AM
Comment:
Hi,
Thanks
Title: Developer   
Name: Craig
Date: 4/3/2007 6:37:24 AM
Comment:
Thank you for taking the time to write this article.
Is there a method for performing the schema validation without referencing the schema (xsd) file separately?
My XML files all contain lines similar to this:
xsi:noNamespaceSchemaLocation="LocalSchema.xsd"
for local schema's or http references for remote schema's.
Title: Developer   
Name: Ysn
Date: 3/28/2007 3:44:10 PM
Comment:
Thanks

Product Spotlight
Product Spotlight 






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/21/2009 12:20:24 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search