Sto eseguendo il porting di un codice su ASP.NET 5 e desidero eseguire il targeting DNX Core 5.0. Tuttavia, sto riscontrando problemi nell'individuare i tipi necessari per convalidare un documento XML rispetto a un documento XSD.Come convalidare XML contro XSD (documenti separati) in DNX Core 5.0 (ASP.NET 5)?
Ecco il codice:
var xsdStream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(xsdPath);
using (XmlReader xsd = XmlReader.Create(xsdStream))
{
XmlSchemaSet schema = new XmlSchemaSet();
schema.Add(null, xsd);
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.ValidationType = ValidationType.Schema;
xmlReaderSettings.Schemas.Add(schema);
using (XmlReader xmlReader = XmlReader.Create(xmlPath, xmlReaderSettings))
{
try
{
while (xmlReader.Read());
}
catch (Exception ex)
{
throw new Exception(string.Format(Resources.Messages.XmlValidationFailed, xmlPath), ex);
}
}
}
Come potete vedere, tutto quello che voglio è quello di fermare il primo errore e genera un'eccezione che indica ciò che l'errore è.
I problemi sono:
- Il
XmlSchemaSet
classe non esiste nelSystem.Xml.Schema
namespace (o in qualsiasi altro luogo ho trovato). - Le proprietà
XmlReaderSettings.ValidationType
eXmlReaderSettings.Schemas
non esistono.
Ho controllato il MSDN Documentation che ha un approccio leggermente diverso. Tuttavia, come prima XmlSchemaSet
non esiste, e nemmeno lo fa XDocument.Validate()
. Ho anche cercato alcuni degli ASP.NET projects per un esempio ma non riesco a trovarne nessuno.
Quali funzionalità (se esistono) esistono in DNX Core 5.0 per convalidare XML rispetto a XSD? Preferirei farlo usando i flussi se possibile, ma se assolutamente necessario accetterò un approccio che rilegga l'intero documento alla volta.
Non sembra che XSD sia parte di CoreClr - https://github.com/dotnet/corefx/tree/master/src. Potresti voler inserire un bug nel loro repository ... – Pawel
@Pawel - Grazie per il suggerimento. Ha aperto un nuovo numero [qui] (https://github.com/dotnet/corefx/issues/3633). – NightOwl888
@Pawel potrebbe voler modificare il tuo commento per rispondere in modo che possa essere votato e accettato. –