Liquid XML Data Binder 2019
Error Handling
Liquid XML Objects (C#, Visual Basic .Net) > Using the Generated Code > Error Handling

Error handling when reading XML data

Errors in the structure of the XML data (no well formed) cause System.Xml.XmlException to be thrown.

Errors encountered when de-serializing an XML document are reported via the LxDeserializerErrorHandler Delegate. You can configure your own handler by setting the ErrorHandler Property on the LxReaderSettings Class.

Reading data from the generated classes
Copy Code
public void Read()
{
    LxSerializer<BookstoreElm> serializer = new LxSerializer<BookstoreElm>();

    LxReaderSettings lxReaderSettings = new LxReaderSettings();
    lxReaderSettings.ErrorHandler = MyDeserializerErrorHandler;

    BookstoreElm bookstore = serializer.Deserialize(@"..\..\BookstoreSample.xml", lxReaderSettings);
}

public void MyDeserializerErrorHandler(string msg, LxErrorSeverity severity, LxErrorCode errorCode, TextLocation location, object targetObject)
{
    if (severity == LxErrorSeverity.Error)
        throw new Exception(msg);
    else if (severity == LxErrorSeverity.Warning)
        Console.WriteLine(msg);
}

Broadly speaking errors can be dealt with in 3 ways :-

Continuing execution after an error (i.e. not throwing an exception within the error handler) may result in further errors and node being classified as unexpected.

 

Error handling when Writing XML data

Errors encountered when serializing a the XML Data Model into XML are reported via the LxSerializerErrorHandler Delegate. You can configure your own handler by setting the ErrorHandler Property on the LxWriterSettings Class.

Writing data from a generated classes
Copy Code
public void Write()
{
    BookstoreElm bookstore = new BookstoreElm();
    BookTypeCt book = new BookTypeCt();
    book.Seq.Title = "Artemis";
    book.Seq.Author.Seq.First_Name = "Andy";
    book.Seq.Author.Seq.Last_Name = "Weir";
    book.Seq.Genre = BookTypeCt.GenreEnum.Science_Fiction;
    book.Price = 7.99;
    book.ISBN = "978-1-78-503025-3";
    book.Publicationdate = LxDateTime.CreateDate(2017, 4, 23);
    bookstore.Seq.Book.Add(book);

    LxSerializer<BookstoreElm> serializer = new LxSerializer<BookstoreElm>();
    LxWriterSettings lxWriterSettings = new LxWriterSettings();
    lxWriterSettings.ErrorHandler = MySerializerErrorHandler;

    serializer.Serialize(@"..\..\BookstoreSample.xml", bookstore, lxWriterSettings);
}

public void MySerializerErrorHandler(string msg, LxErrorSeverity severity, LxErrorCode errorCode, object targetObject)
{
    if (severity == LxErrorSeverity.Error)
        throw new Exception(msg);
    else if (severity == LxErrorSeverity.Warning)
        Console.WriteLine(msg);
}

Broadly speaking errors can be dealt with in 2 ways :-

 

See Also