From cadf28c58f7ecc2014943711bf637893b5d30aa8 Mon Sep 17 00:00:00 2001 From: Philemon Eichin Date: Sun, 18 Jun 2023 21:45:14 +0200 Subject: [PATCH] Log line numbers for `XmlSchemaException` --- XmlSchemaClassGenerator/Generator.cs | 31 +++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/XmlSchemaClassGenerator/Generator.cs b/XmlSchemaClassGenerator/Generator.cs index 54e4146f..052ad934 100644 --- a/XmlSchemaClassGenerator/Generator.cs +++ b/XmlSchemaClassGenerator/Generator.cs @@ -335,7 +335,14 @@ public void Generate(IEnumerable files) var ex = e.Exception as Exception; while (ex != null) { - Log?.Invoke(ex.Message); + if(ex is XmlSchemaException xmlException) + { + Log?.Invoke(FormatXmlSchemaExeption(xmlException)); + } + else + { + Log?.Invoke(ex.Message); + } ex = ex.InnerException; } }; @@ -378,5 +385,27 @@ public void Generate(XmlSchemaSet set) writer.Write(ns); } } + + private string FormatXmlSchemaExeption(XmlSchemaException e) + { + StringBuilder sb = new StringBuilder(e.Message); + + if(!string.IsNullOrEmpty(e.SourceUri)) + { + sb.AppendFormat(" at {0}:{1}:{2}", + e.SourceUri, + e.LineNumber, + e.LinePosition); + } + else if(e.SourceSchemaObject != null) + { + sb.AppendFormat(" at {0}:{1}:{2}", + e.SourceSchemaObject.SourceUri, + e.SourceSchemaObject.LineNumber, + e.SourceSchemaObject.LinePosition); + } + + return sb.ToString(); + } } } \ No newline at end of file