Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing error when nullable elements with empty value and "xsi:nil"=true attribute #54

Closed
sloncho opened this issue Aug 4, 2023 · 3 comments · Fixed by #60
Closed
Labels
bug Something isn't working

Comments

@sloncho
Copy link

sloncho commented Aug 4, 2023

It appears that the generated code for nullable property getter only inspects if the element exists. It fails when the element exists with attribute "xsi:nil"=true.

Example:
xsd: <xsd:element name="createDate" type="xsd:dateTime" minOccurs="0" nillable="true" />

Incoming xml:

<someType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
      <createDate xsi:nil="true"/>
 </someType>

When accessing the generated createDate property, it throws, as it finds the element, and tries to parse the empty value to DateTime.

As XDocument/XElement are not schema-aware, I'm not even sure which approach for solving this will be more appropriate - inspecting for the nil=true, or using the XElement.IsEmpty to detect the null value.

@mamift mamift added the bug Something isn't working label Sep 15, 2023
@hieu-informatics
Copy link

hieu-informatics commented Jan 10, 2024

Do you have any update for this serious bug? I suppose GetElement should apply the same check on nil attribute as done for SetElement.

    internal void SetElement(XName name, object value, bool addToExisting, XmlSchemaDatatype datatype, Type elementBaseType)
    {
        XElement untyped = GetUntyped();
        CheckXsiNil(untyped);
...

    private void CheckXsiNil(XElement parentElement)
    {
        XAttribute xAttribute = parentElement.Attributes(XName.Get("nil", "http://www.w3.org/2001/XMLSchema-instance")).FirstOrDefault();
        if (xAttribute != null && xAttribute.Value == "true")
        {
            xAttribute.Remove();
        }
    }

@hieu-informatics
Copy link

@mamift Do you have some plan for this bug fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants