Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 2.30 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why don't I get schema information when using an XPathDocument with a validating XmlReader?
  2. using System;
  3.  
  4. using System.Xml;
  5. using System.Xml.XPath;
  6. using System.Xml.Schema;
  7.  
  8. namespace XmlSchemaTest
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var xpathDocument = ReadAndValidateXmlFile(@"c:tempxmltest.xml", @"C:tempxmltest.xsd");
  15.  
  16.             if (xpathDocument == null)
  17.                 Console.WriteLine("Cannot open document.");
  18.             else
  19.             {
  20.                 var xpathNavigator = xpathDocument.CreateNavigator();
  21.  
  22.                 var xpathNodeIterator = xpathNavigator.Select("/Data/FloatValue");
  23.  
  24.                 if (!xpathNodeIterator.MoveNext())
  25.                     Console.WriteLine("No matches.");
  26.                 else
  27.                 {
  28.                     Console.WriteLine("SchemaInfo is " + ((xpathNodeIterator.Current.SchemaInfo == null) ? "null" : "not null"));
  29.                     Console.WriteLine("XmlType is " + ((xpathNodeIterator.Current.XmlType == null) ? "null" : "not null"));
  30.                     Console.WriteLine("TypedValue is " + xpathNodeIterator.Current.TypedValue.GetType().ToString());
  31.                 }
  32.             }
  33.  
  34.             Console.ReadLine();
  35.         }
  36.  
  37.         private static XPathDocument ReadAndValidateXmlFile(string xmlPath, string xsdPath)
  38.         {
  39.             var xmlReaderSettings = new XmlReaderSettings();
  40.  
  41.             xmlReaderSettings.ValidationType = ValidationType.Schema;
  42.  
  43.             xmlReaderSettings.Schemas.Add(targetNamespace: null, schemaUri: xsdPath); // null means use the target namespace referenced by the XML document
  44.  
  45.             bool anyValidationErrors = false;
  46.  
  47.             xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler((object sender, ValidationEventArgs args) => anyValidationErrors = true );
  48.  
  49.             using (var xmlReader = XmlReader.Create(xmlPath, xmlReaderSettings))
  50.             {
  51.                 // Note: a document will be created even if there are validation errors,
  52.                 // but for our purposes we'll discard 'invalid' documents.
  53.  
  54.                 var xpathDocument = new XPathDocument(xmlReader);
  55.  
  56.                 if (anyValidationErrors)
  57.                     return null;
  58.                 else
  59.                     return xpathDocument;
  60.             }
  61.         }
  62.     }
  63. }