Answered by:
Problem validating xml with xsd

Question
-
Hi All. I have an app that uses an xml file to store user settings. I had to change the schema for the file due to some mods, so I need to check the existing settings file (if there is one) to see if it matches the updated schema (xsd) file. If it doesn't match, I want to delete the old file and create a new one in the new format.
I created a method that is supposed to check the existing settings file and validate it against the schema file. My problem is that even when I provide an xml file that is completely different, the validation doesn't return any errors.
Any thoughts on what I'm missing??? Thanks for your help!
VS2005, .NET 2.0
___________________________________________
Here is the xml file "settings.xml":<?xml version="1.0" encoding="UTF-8"?> <stuff ExpImpPath="C:\BizTalk" server="boi05pc095" /> Here is the schema file "SettingsSchema.xsd":
<?xml version="1.0" encoding="utf-16"?> <xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://www.simplot.com/Simplot.Enterprise.BizTalkSSOTool/SettingsSchema.xsd" elementFormDefault="qualified" targetNamespace="http://www.simplot.com/Simplot.Enterprise.BizTalkSSOTool/SettingsSchema.xsd" id="SettingsSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="settings"> <xs:complexType> <xs:sequence maxOccurs="1"> <xs:element name="configuration"> <xs:complexType> <xs:attribute name="ssoServer" type="xs:string" /> <xs:attribute name="EDIClearEnabledFlag" type="xs:boolean" /> <xs:attribute name="EDIRepositoryServer" type="xs:string" /> </xs:complexType> </xs:element> <xs:element name="users"> <xs:complexType> <xs:sequence maxOccurs="unbounded"> <xs:element name="user"> <xs:complexType> <xs:attribute name="username" type="xs:string" /> <xs:attribute name="ExpImpPath" type="xs:string" /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
And here is my validation code:
public bool ValidateSettingsFile() { string fileLocation = Application.StartupPath + "\\settings.xml"; string schemaLocation = Application.StartupPath + "\\SettingsSchema.xsd"; string schemaNamespace = "http://www.simplot.com/Simplot.Enterprise.BizTalkSSOTool/SettingsSchema.xsd"; try { XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(schemaNamespace, schemaLocation); settings.ValidationType = ValidationType.Schema; XmlReader xmlReader = XmlReader.Create(fileLocation, settings); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlReader); ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationError); xmlDoc.Validate(eventHandler); foreach (XmlNode node in xmlDoc) { if ((node.NodeType == XmlNodeType.Attribute) || (node.NodeType == XmlNodeType.Element)) { xmlDoc.Validate(eventHandler, node); } } if (!xmlSettingsFileValid) { //delete the old style settings file System.IO.File.Delete(fileLocation); createSettingsFile(); return false; } else { return true; } } catch (System.IO.FileNotFoundException e) { string filename = System.IO.Path.GetFileName(e.FileName); if (filename != "SettingsSchema.xsd") { createSettingsFile(); } return false; } } private void ValidationError(object sender, ValidationEventArgs e) { xmlSettingsFileValid = false; } - Moved by jack 321 Monday, July 14, 2008 8:24 AM off topic for C# general
Thursday, July 10, 2008 3:08 PM
Answers
-
Discussion in Data Platform Development
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, December 22, 2011 2:21 PM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, December 22, 2011 2:23 PM
Monday, July 14, 2008 8:24 AM -
This was moved from the C# forum.
Let me know if you want me to move it into a specific forum.
This is regarding your code:
<?xml version="1.0" encoding="utf-16"?> <xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://www.simplot.com/Simplot.Enterprise.BizTalkSSOTool/SettingsSchema.xsd" elementFormDefault="qualified" targetNamespace="http://www.simplot.com/Simplot.Enterprise.BizTalkSSOTool/SettingsSchema.xsd" id="SettingsSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="settings"> <xs:complexType> <xs:sequence maxOccurs="1"> <xs:element name="configuration"> <xs:complexType> <xs:attribute name="ssoServer" type="xs:string" /> <xs:attribute name="EDIClearEnabledFlag" type="xs:boolean" /> <xs:attribute name="EDIRepositoryServer" type="xs:string" /> </xs:complexType> </xs:element>
Ed Price a.k.a User Ed, Microsoft Experience Program Manager (Blog, Twitter, Wiki)- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, December 22, 2011 2:23 PM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, December 22, 2011 2:23 PM
Thursday, December 22, 2011 2:23 PM
All replies
-
Try my validation code from here.
MCDBA, MCSD, MCITP http://sharpsource.blogspot.com/Thursday, July 10, 2008 3:32 PM -
Hi boban.s,
Thanks for your help! Unfortunately, it's still not working. I had to fiddle with your code a bit since I'm reading the xml from a file instead of as a string within the code, but I think I have it matched up. Here's my revised code. What am I missing? Thanks!
public bool ValidateSettingsFile() { string fileLocation = Application.StartupPath + "\\settings.xml"; string schemaLocation = Application.StartupPath + "\\SettingsSchema.xsd"; string schemaNamespace = "http://www.simplot.com/Simplot.Enterprise.BizTalkSSOTool/SettingsSchema.xsd"; try { XmlReader schemaReader = XmlReader.Create(schemaLocation); XmlReader xmlReader = XmlReader.Create(fileLocation); XmlSchema xmlSchema = new XmlSchema(); xmlSchema = XmlSchema.Read(schemaReader, ValidationError); XmlReaderSettings ReaderSettings = new XmlReaderSettings(); ReaderSettings.ValidationType = ValidationType.Schema; ReaderSettings.Schemas.Add(schemaNamespace, schemaLocation); ReaderSettings.ValidationEventHandler += new ValidationEventHandler(ValidationError); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlReader); XmlNodeReader nodeReader = new XmlNodeReader(xmlDoc); using (XmlReader xmlValReader = XmlReader.Create(nodeReader, ReaderSettings)) { while (xmlValReader.Read()) { // Empty loop to find errors } } schemaReader.Close(); xmlReader.Close(); if (!xmlSettingsFileValid) { //delete the old style settings file System.IO.File.Delete(fileLocation); createSettingsFile(); return false; } else { return true; } } catch (System.IO.FileNotFoundException e) { string filename = System.IO.Path.GetFileName(e.FileName); if (filename != "SettingsSchema.xsd") { createSettingsFile(); } return false; } } Thursday, July 10, 2008 5:13 PM -
Discussion in Data Platform Development
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, December 22, 2011 2:21 PM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, December 22, 2011 2:23 PM
Monday, July 14, 2008 8:24 AM -
This was moved from the C# forum.
Let me know if you want me to move it into a specific forum.
This is regarding your code:
<?xml version="1.0" encoding="utf-16"?> <xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://www.simplot.com/Simplot.Enterprise.BizTalkSSOTool/SettingsSchema.xsd" elementFormDefault="qualified" targetNamespace="http://www.simplot.com/Simplot.Enterprise.BizTalkSSOTool/SettingsSchema.xsd" id="SettingsSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="settings"> <xs:complexType> <xs:sequence maxOccurs="1"> <xs:element name="configuration"> <xs:complexType> <xs:attribute name="ssoServer" type="xs:string" /> <xs:attribute name="EDIClearEnabledFlag" type="xs:boolean" /> <xs:attribute name="EDIRepositoryServer" type="xs:string" /> </xs:complexType> </xs:element>
Ed Price a.k.a User Ed, Microsoft Experience Program Manager (Blog, Twitter, Wiki)- Proposed as answer by Ed Price - MSFTMicrosoft employee Thursday, December 22, 2011 2:23 PM
- Marked as answer by Ed Price - MSFTMicrosoft employee Thursday, December 22, 2011 2:23 PM
Thursday, December 22, 2011 2:23 PM