473,473 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

validating an xml document.

i wrote a program sometimes to validate my xml file against the schema using c#.net but i found out that whether there was error or no error, it will always say the document is valid even if i specify the wrong file name. i want you to give me a snippet of code and the xml file to see how to validate in C#.net
Sep 17 '06 #1
1 1515
kartic03
6 New Member
protected void Page_Load(object sender, EventArgs e)
{
try
{
string a,b,error;
strXml = "Example.xml";
strXsd = "Example.xsd";
XmlSchemaCollection sc = new XmlSchemaCollection();
sc.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
sc.Add(null, strXsd);

//XmlTextReader textReader = new XmlTextReader(strXml);
//XmlValidatingReader validatingReader = new XmlValidatingReader(textReader);
//validatingReader.ValidationType = ValidationType.Schema;
XmlSchema schema = XmlSchema.Read(new XmlTextReader(strXsd), null);
schema.SourceUri = strXsd;
XmlValidatingReader validatingReader = new XmlValidatingReader(new
XmlTextReader(strXml));
validatingReader.Schemas.Add(schema);
validatingReader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
while (validatingReader.Read()) ;
a = "Success";
error="Error";
if(error==a)
{
Response.Write("Invalid File "+b);
}
else
{
Response.write("Xml IS valid");
}

}
catch(Exception ex)
{
a = "Error";
b = exc.Message;
}

}
private void ValidationCallBack(object sender,System.Xml.Schema.ValidationEventArgs e)
{
if (e.Message != "")
{
a = "Error";
b = e.Message;
}
}



I hope this will help you.
Dec 6 '06 #2

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

Similar topics

1
by: Honza Pazdziora | last post by:
Hello, I'm processing documents that can have any element names in them. The only restriction is placed on the attributes of these elements, each of them can only have two attributes of given...
1
by: scorpion | last post by:
I have this problem that an xml instance is validated correctly by xml tools, but not with my simple code, by setting the validating flag to true. --------------- Schema...
2
by: Stefan Ram | last post by:
I have found a nice example of how to create an XML document using DOM with Java: http://www.cafeconleche.org/books/xmljava/chapters/ch10s03.html Now, my problem is: When I modify the element...
2
by: Will | last post by:
I have been having problems validating an XForms document against the XForms schema located at http://www.w3.org/MarkUp/Forms/2002/XForms-Schema.xsd. I have reduced the XForm to its bare bones...
6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
2
by: and | last post by:
Hi I have been validating all day most things are cool but I cant get by this problem. One I have listed the script (JAVASCRIPT ) in all the right placesnot a prob but the validator insists...
0
by: jean-gert nesselbosch | last post by:
hello everybody, does anybody know of standardization efforts concerning the output of w3cSchema/relaxNG-validating parsers ? I use libxml2 for validation-purposes (w3c-Schema and relaxNG) and...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
1
by: Chris Lieb | last post by:
I have an XML Schema file that I know is correct becuase I currently use it in a VB6 program to validate XML documents. Also, if I load an XML file into VS2005 that is not valid against this...
2
by: tarunprabhu | last post by:
Hi, I am using Xerces 2.8. When I try to validate a file which is in the same folder as the xsd, it works fine. But if I try to validate an XML file which is in some other folder, it gives me...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.