0

I'm using c# and trying to replicate some functionality that I wrote previously in VB6.

It should be simple: I have XML files which contain this statement: xsi:noNamespaceSchemaLocation="Schemas\mySchema.xsd"

I would like to be able to use XMLDocument to both parse and validate the XML file against the schema file referenced above.

I should not have to provide a path and file name separately since they are already contained within the XML file.

I do not want to use the XMLReader if possible.

My previous VB6 code could do this and would generate appropriate errors if the schema file did not exist/could not be found, or if the contents of the XML file failed validation.

How can I accomplish this in C#?

wayneh
  • 4,393
  • 9
  • 35
  • 70
  • Why do you not want to use XMLReader? – Progman Nov 03 '17 at 18:15
  • @Progman - I'll need to use an XMLDocument when I access my XML, so I was hoping to avoid another xml object. Isn't the XMLDocument capable of what I'm trying to do? – wayneh Nov 03 '17 at 18:17
  • 1
    use XmlReaderSettings – Alexan Nov 03 '17 at 18:22
  • @Alexan - Thanks but I already stated I want to avoid using XMLReader – wayneh Nov 03 '17 at 18:23
  • so, don't use it, just use XmlReaderSettings – Alexan Nov 03 '17 at 18:25
  • 1
    @wayneh - *I want to avoid using XMLReader* -- When loading an `XmlDocument` internally an `XmlReader` is used anyway. See https://msdn.microsoft.com/en-us/library/a8ta6tz4(v=vs.110).aspx. So if you create a validating `XmlReader` you can load and validate simultaneously. Is that what you want? Or do you want to validate after you load? – dbc Nov 03 '17 at 18:25
  • and use XDocument, not XMLDocument – Alexan Nov 03 '17 at 18:27
  • @Alexan - can you be more specific? Perhaps point me to some examples? – wayneh Nov 03 '17 at 18:38
  • 1
    You could create an `XmlReader` as shown in [this answer](https://stackoverflow.com/a/2553468) and then pass it to [XmlDocument.Load(XmlReader)](https://msdn.microsoft.com/en-us/library/a8ta6tz4(v=vs.110).aspx) or [`XDocument.Load(XmlReader)`](https://msdn.microsoft.com/en-us/library/bb356384(v=vs.110).aspx). – dbc Nov 03 '17 at 18:41
  • you can use Validate method: https://msdn.microsoft.com/en-us/library/bb354954(v=vs.110).aspx – Alexan Nov 03 '17 at 19:08

1 Answers1

1

OK, after the comments above this is basically what I ended up with even though I had not wanted to use XmlReader:

...    
xmlString = System.IO.File.ReadAllText("myXMLDoc.xml");
XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationEventHandler += ValidationHandler;
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings |
                       XmlSchemaValidationFlags.ProcessIdentityConstraints |
                       XmlSchemaValidationFlags.ProcessSchemaLocation |
                       XmlSchemaValidationFlags.ProcessInlineSchema;

StringReader r = new StringReader(xmlString);
XmlReader validatingReader = XmlReader.Create(r, settings);
XmlDoc = new XmlDocument();
XmlDoc.Load(validatingReader);
...

private static void ValidationHandler(object sender, ValidationEventArgs e)
{
  if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
    {
      validationErr += "XML Parse Error Line: " +
                         e.Exception.LineNumber + " Position: " +
                         e.Exception.LinePosition + " Message: " +
                         e.Exception.Message + Environment.NewLine;
     }
}

Again, if there's a way to minimize the above code I'd appreciate some suggestions.

I haven't yet looked into XDocument yet as a replacement for XmlDocument.

wayneh
  • 4,393
  • 9
  • 35
  • 70
  • Rather than reading into an intermediate `string` I'd suggest reading directly from the file using a `StreamReader`. If the XML file is large the string can end up going in the [large object heap](https://stackoverflow.com/questions/8951836/why-large-object-heap-and-why-do-we-care) and cause memory usage or performance problems down the road. – dbc Nov 03 '17 at 20:58
  • @dbc - Thanks, but my xml data is input to my code as a string from an external source - there will be no file. With the nature of the incoming xml, I suspect it will never go over 1 or 2mb in size. – wayneh Nov 04 '17 at 15:28