| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| hi @all, I'm trying to validate a XML against a schema using the example from the MSDN: http://msdn.microsoft.com/en-us/libr...ationtype.aspx In my case this seems to validates my file, because the ValidationCallBack handler is not called. But the file is invalid due to a wrong fixed attribute. I don't know why, so any clue is appreciated. My GPX (GPS Exchange Format) file: <?xml version="1.0"?> <gpx version="1.0" creator="Holux Utility" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd"> <wpt lat="48.342567" lon="10.865602"> <ele>137.59</ele> <time>2008-09-03T15:17:23Z</time> <name><![CDATA[Point 0]]></name> </wpt> <wpt lat="48.342548" lon="10.865611"> <ele>137.72</ele> <time>2008-09-03T15:17:24Z</time> <name><![CDATA[Point 1]]></name> </wpt> </gpx> My code looks like this (.Net 3.5, dlgFileOpen.FileName points to the file above): XmlSchemaSet sc = new XmlSchemaSet(); sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd"); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas = sc; settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); XmlReader reader = XmlReader.Create(dlgFileOpen.FileName, settings); while (reader.Read()) ; mfG --> stefan <-- |
|
#2
| |||
| |||
| Stefan Hoffmann wrote: > <gpx > version="1.0" > creator="Holux Utility" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xmlns="http://www.topografix.com/GPX/1/0" So here the namespace URI is http://www.topografix.com/GPX/1/0 while later on you add a schema for > XmlSchemaSet sc = new XmlSchemaSet(); > sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd"); http://www.topografix.com/GPX/1/1 which is different. Make sure you add a schema for the namespace your XML instance document uses, otherwise validation will only emit some warnings that no matching schema is found and your code > XmlReaderSettings settings = new XmlReaderSettings(); > settings.ValidationType = ValidationType.Schema; > settings.Schemas = sc; > settings.ValidationEventHandler += new > ValidationEventHandler(ValidationCallBack); will not even so those warnings as you do not have the ValidationFlags set on your settings object to ReportValidationWarnings. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ |
|
#3
| |||
| |||
| hi Martin, Martin Honnen wrote: > will not even so those warnings as you do not have the ValidationFlags > set on your settings object to ReportValidationWarnings. Thanks. btw, is there any idiom to detect the necessary schema for a XML file? In my GPX case there are two schemas. I'm considering using this for the XmlReaderSettings.Schemas: XmlSchemaSet sc = new XmlSchemaSet(); sc.Add("http://www.topografix.com/GPX/1/0", "Resources\\gpx-1.0.xsd"); sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd"); So when the validation handler is not called, then one schema applies to the file. Is this correct? mfG --> stefan <-- |
|
#4
| |||
| |||
| Stefan Hoffmann wrote: > btw, is there any idiom to detect the necessary schema for a XML file? > In my GPX case there are two schemas. > > I'm considering using this for the XmlReaderSettings.Schemas: > > XmlSchemaSet sc = new XmlSchemaSet(); > sc.Add("http://www.topografix.com/GPX/1/0", "Resources\\gpx-1.0.xsd"); > sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd"); > > So when the validation handler is not called, then one schema applies to > the file. The validating parser will look at the namespace of the root element of the XML document to be validated and then check for a schema with a matching targetNamespace in its XmlSchemaSet. If it does not find a matching schema then a warning is issued. If a matching schema is found the parser checks whether there is a top level defintion for the root element and validates against that definition, reporting errors as needed to the handler. If there is no top level definition for the root element then this is also an error. So the above should work. Note that you can shorten the code, there is no need to explicitly create and assing an XmlSchemaSet, you can simply call settings.Schemas.Add. And passing in the schema URI to the Add method is not necessary, if you pass in null for that parameter then it simply uses the targetNamespace of the schema passed in as the second argument. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ |
|
#5
| |||
| |||
| hi Martin, Martin Honnen wrote: >> XmlSchemaSet sc = new XmlSchemaSet(); >> sc.Add("http://www.topografix.com/GPX/1/0", "Resources\\gpx-1.0.xsd"); >> sc.Add("http://www.topografix.com/GPX/1/1", "Resources\\gpx-1.1.xsd"); > Note that you can shorten the code, there is no need to explicitly > create and assing an XmlSchemaSet, you can simply call > settings.Schemas.Add. And passing in the schema URI to the Add method is > not necessary, if you pass in null for that parameter then it simply > uses the targetNamespace of the schema passed in as the second argument. Ah, cool. Thanks a lot. mfG --> stefan <-- |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.