Friday 15 March 2013

c# - XDocument.Validate is always successful -


I have a schema file that does not define any target namespace, that is, its definition looks like this:

  & lt; Xs: schema xmlns: xs = "http://www.w3.org/2001/XMLSchema" elementFormDefault = "qualified" & gt; & Lt ;! - elements, characteristics, etc. - & gt; & Lt; / XS: Schema & gt;   

The related XML looks like this:

  & lt; Document p1: CRC = "0" page 1: date = "19 00-01-01 0101: 01: 01 + 01: 00" P1: name = "test" page 1: status = "new" xmlns: p1 = "Http://www.tempuri.org/pdms.xsd" xmlns = "http: //www.tempuri .org / pdms.xsd" & gt; & Lt; P1: Document Page 1: Date = "2010-12-23 T0: 59: 45" Page 1: Error Code = "0" Page 1: Error Message = "P1: Number =" TEST00001 "Page 1: Status = "New" /> & Lt; / Documents & gt;   

With the validation of this XML against schema for example, Altova XMLSpy or Oxygen XML Editor fails.

Although my validation in C # (.NET 4.0) does not fail. XML is processed as a XDocument object, if I understand correctly, then XDocument.Validate () does a loose verification, if the schema No namespace has been found. Thus validation is not valid but how can I apply a "strict" verification for XDocument ?

In this way I try to validate XML:

  public static zero value adapter Xml (XDocument xml, string xsdFilename) {XmlReaderSettings Settings = New XmlReaderSettings () ; XmlSchemaSet schematic = new XmlSchemaSet (); SchemaSet.Add (string.free, xsdFilename); Settings.ValidationType = Validation Type Sema; Settings.ValidationFlags | = XmlSchemaValidationFlags.ReportValidationWarnings; Settings.ValidationEventHandler + = New ValidationEventHandler (Validation Callback); Xml.Validate (schemaset, validation callback); } Private Static Zero validation callback (Object Sender, ValidationAventAGRG AGR) {if (args.Severity == XmlSeverityType.Warning) {// Warning stuff ...} else if (args.Severity == XmlSeverityType.Error) {// Do error stuff ...}}    

I'm not sure that it is possible to use Is valid method; If you use a valid XmlReader on XDocument, where validation alerts are set to emit verification alerts, such as

  XDocument doc = XDocument.Load ("../ ... XMLFile1.xml "); XmlSchemaSet schematic = new XmlSchemaSet (); Schematic Add (empty, "../../XMLSchema1.xsd"); XmlReaderSettings xrs = New XmlReaderSettings (); Xrs.ValidationType = Validation Type. Sema; Xrs.ValidationFlags | = XmlSchemaValidationFlags.ReportValidationWarnings; Xrs.Schemas = SchemaSet; Xrs.ValidationEventHandler + = (O, S) = & gt; {Console.WriteLine ("{0}: {1}", s.Severity, s.Message); }; (XmlReader xr = XmlReader.Create (doc.CreateReader (), xrs) {while (xr.Read ()) {}}   

then validationAventhandler emits a warning for each There is no schema information for the node, so your verification and handler may check such alerts. But you can also compare DOS. Route: Name the schema with namepad before calling the valid method namespace valid.

No comments:

Post a Comment