How to validate an XML with an XSD using LINQ -


edit2: problem seems xsd. validates pretty every xml. can't post xsd on here though. why every xml valid xsd?

edit: found similar excample in answer on here. same issue, finds no errors no matter xml , xsd compare. if use random diffrent xsd keeps saying fine.

i found lot of examples doing without linq, how linq?

i used google find example seems skip validation of time validating every xml. (it once went it, rejecting file haven't been able reproduce it.)

are there better ways of doing or there reason why skip on validation?

public string validatexml2(string xml, string xsd)     {         string message = string.empty;          var ms = new memorystream(encoding.default.getbytes(xml));          // create xml document validate against.         xdocument xdoc = xdocument.load(ms, loadoptions.preservewhitespace);         xmlschemaset schema = new xmlschemaset();          bool iserror = new bool();  // defaults false.         int counterror = 1;         // counts number of errors have generated.         stream xsdmemorystream = new memorystream(encoding.default.getbytes(xsd));          // add schema file want validate against.         schema.add(xmlschema.read                 (xsdmemorystream,                     new validationeventhandler((sender, args) =>                     {                         message = args.exception.message;                     })                 ));          // call validate , use lambda expression extended method!         // don't love .net 3.5 , linq...         xdoc.validate(schema, (sender, e) =>         {             switch (e.severity)             {                 case xmlseveritytype.error:                     console.writeline("error {0} generated.", counterror);                     break;                 case xmlseveritytype.warning:                     console.writeline("warning {0} generated.", counterror);                     break;             }             console.writeline(sender.gettype().name);             console.writeline("\r\n{0}\r\ntype {1}\r\n", e.message,                                                          e.severity.tostring());              console.writeline("-".padright(110, '-'));             counterror++;             iserror = true; // if error fires, flag handle once call complete.         }           , true); // true tells validate call populate post-schema-validation         // need later, if want dive littel deeper...          if (iserror == true) // error has been flagged.  lets see errors generated.             console.writeline("you friend have {0} error(s), what?", counterror);         else             console.writeline("you rock! no errors...");          console.write("\r\n\r\npress enter end");         console.readkey();           return message;     } 

credits , original example

apparently using linq xml have have schemas targetnamespace match namespace of xml checking, because validate method looks through collection of schemas 1 validating documents namespace. if 1 not found document validated.

check out this link more info


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -