472,954 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Validating XML

I have to validate xml file against xsd file using c#.
XmlReader only check for the well formated doucment but i want to
check for every thing like space,datatype and any extra text in xml
file.

pls help me do this

tell me some library if there is any to validate xml document.
Jun 27 '08 #1
3 1633
Mahain wrote:
I have to validate xml file against xsd file using c#.
XmlReader only check for the well formated doucment but i want to
check for every thing like space,datatype and any extra text in xml
file.
Use an XmlReader with XmlReaderSettings set up for validation:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, "schema.xsd");
bool valid = true;
settings.ValidationEventHandler += delegate(object sender,
ValidationEventArgs vargs)
{
if (vargs.Severity == XmlSeverityType.Error)
{
valid = false;
}
Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message);
};

using (XmlReader reader = XmlReader.Create(@"doc.xml", settings))
{
while (reader.Read()) {}
}
Console.WriteLine("Document is {0}.", valid ? "valid": "not valid");

See also the MSDN section:
<URL:http://msdn.microsoft.com/en-us/library/hdf992b8(VS.80).aspx>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
On May 8, 3:55 pm, Martin Honnen <mahotr...@yahoo.dewrote:
Mahain wrote:
I have to validate xml file against xsd file using c#.
XmlReader only check for the well formated doucment but i want to
check for every thing like space,datatype and any extra text in xml
file.

Use an XmlReader with XmlReaderSettings set up for validation:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, "schema.xsd");
bool valid = true;
settings.ValidationEventHandler += delegate(object sender,
ValidationEventArgs vargs)
{
if (vargs.Severity == XmlSeverityType.Error)
{
valid = false;
}
Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message);
};

using (XmlReader reader = XmlReader.Create(@"doc.xml", settings))
{
while (reader.Read()) {}
}
Console.WriteLine("Document is {0}.", valid ? "valid": "not valid");

See also the MSDN section:
<URL:http://msdn.microsoft.com/en-us/library/hdf992b8(VS.80).aspx>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
same result as XMLReader
Jun 27 '08 #4

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

Similar topics

6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
0
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem. The event would fire when clicking on another...
2
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
4
by: easoftware | last post by:
I am using VS .Net 2003 and VB. I have an app with one parent and two Mdi child forms. I need to validate data in the Mdi form. The Form.Validating event works when I try to close a Mdi form,...
6
by: Ryan | last post by:
I have a windows form that I want to force validation on controls (text boxes) when the user clicks a "Save" button. The only way I've found to do this is to cycle through every control and call...
16
by: Al Santino | last post by:
Hi, It appears displaying a messagebox in a validating event will cancel the subsequent event. In the program below, button 2's click event doesn't fire if you open a dialog box in button 1's...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.