XDocument.Validate how to keep validating after errors?

264 Views Asked by At

i have a little problem and can´t figure out how to resolve it. I am Validating an XDocument against a schema and i get all the nodes which have error. But the Validation process doesnt go deeper after finding an error.

     _document.Validate(_schema, (o, e) =>
        {
            XElement xEle = null;
            if (o is XAttribute)
                xEle = (o as XAttribute).Parent;
            if (o is XElement)
                xEle = o as XElement;
            if (xEle == null)
            {
                Debug.WriteLine(o.ToString());
                return;
            }
            _elemtList.Add(o as XElement);
        });

My problem is like following

<Car>
<CarName></CarName>
<CarInteriour>
    <CarInteriorColor>Red</CarInteriorColor>
</CarInteriour>
</Car>

Lets say this is valid. If i Change the following to

<Car>
<CarInteriour>
    <CarInteriorColor></CarInteriorColor>
</CarInteriour>
</Car>

Here is the CarName tag missing and the color Red. I will only get the error for CarName but not color Red. The validation process seems to skip that structure because it did find an error. Is there a way to still keep validating even if there was an error ?

1

There are 1 best solutions below

0
On

Ok i found a solution which works for me and i am giving you an update because it works pretty neat.

What i am doing now is. I use a foreach for every XElement in my _document.Descendants() and i validate every element. This finds every error in the document multiple times. So what i do is, i check my errorlist if i already did find this error before. My errorlist is a list of my own class errorcontainer which has the found XElement with the Error Message. This way i only add new errors to the list and show this errors on a dialog. The user now can select an error and i will directly jump to the error in my editor.

                ErrorContainer errorContainer = new ErrorContainer(xEle, e.Message);
                if (_errors.Any(error => error.xElement.Equals(errorContainer.xElement) && string.CompareOrdinal(error.errorMessage, errorContainer.errorMessage) == 0))
                {
                    return;
                }
                _errors.Add(errorContainer);

I hope this helps some other people who need help on this issue :)