0

This is really starting to wind me up. I've tried searching all over the net with this but it seems people only want to consume XML files, not build them :S. I'm building an XML file which needs to be compliant with an .xsd referencing the http://www.w3.org/2001/XMLSchema namespace.

One of the elements in the XSD is a datetime which unfortunately may or may not have a value. From what I've read, the correct way to achieve this is to use the nil attribute, but no matter what I do, the validation just moans at me, saying that

'' is not a valid value for type 'date'

I've tried:

elementName.SetAttributeValue(xmlNamespace + "nil", "true"); //(xmlNamespace = pre-declared NS)

and

elementName.SetAttributeValue("nil", "true");

Alongside everything else I can think of, but I just can't get it to validate. Unfortunately I also can't just leave it out as the element doesn't have minOccurs = 0 in the XSD either.

Any ideas?

dymanoid
  • 14,771
  • 4
  • 36
  • 64
Adamski343
  • 39
  • 9
  • thanks for the edit dymanoid, I can never get the code formatting to work. – Adamski343 Mar 10 '20 at 12:14
  • I was fighting the same issue last week for hours. I then discovered I didn't need the namespace. The namespace was set in the parent. When I didn't use the namespace in the child I automatically got the child with no namespace. – jdweng Mar 10 '20 at 12:16
  • What is the XSD for your element? Does it have `nillable="true"`? – canton7 Mar 10 '20 at 12:28
  • hi Canton - maybe im completely misunderstanding this, but doesn't that namespace allow nil for date? – Adamski343 Mar 10 '20 at 13:11
  • @Adamski343 If and only if the XSD has `nillable="true"` IIRC. If you use `xsi:nil="true"` and your XSD doesn't have `nillable="true"`, then that will fail validation – canton7 Mar 10 '20 at 13:20
  • What is `xmlNamespace`? It should be `http://www.w3.org/2001/XMLSchema-instance`. Re whether it's allowed (as @canton7 suggests), [this explains](https://www.oreilly.com/library/view/xml-in-a/0596007647/re166.html). Failing that, it's hard to answer without seeing a [mcve]. – Charles Mager Mar 10 '20 at 16:09

0 Answers0