1

Maybe I'm not doing this correctly. But I'm using MusicXML, along with XSD.exe to generate the C# classes against the DTD.

According to the documentation, along with a sample xml file, the <NOTE> element contains an empty <CHORD> element, if the <NOTE> is a part of a <CHORD>. The code generated looks like this:

[System.Xml.Serialization.XmlElementAttribute("chord", typeof(empty))]

When I deserialize the XML into a c# object, it works great, but the <CHORD> seems to disappear. Does .NET just ignore the CHORD element in this sample XML?

  <note>
    <chord/>
    <pitch>
      <step>E</step>
      <alter>-1</alter>
      <octave>4</octave>
    </pitch>
    <duration>1</duration>
  </note>

Thanks!

kevin
  • 417
  • 4
  • 20
  • what do you mean "`` seems to disappear" when deserializing? – NotMe Mar 21 '13 at 23:14
  • The note c# object does not having anything related to a chord, it does not make it through the deserialization. – kevin Mar 21 '13 at 23:26
  • Are you meaning that when it's *serialized* the output doesn't have the empty chord node? Deserialization is taking the input and creating an object. Serialization is the other way around. – NotMe Mar 21 '13 at 23:33

1 Answers1

3

Do you mean the chord element dissapears when you serialize to XML, as null elements don't serialize by default

If you want to render it as an empty element like

<chord />

you can set use the isnullable property XML Serialization and null value - C#

As linked in another question you might want to have a look at this article about the representation of null in XML Schema files:

http://www.ibm.com/developerworks/xml/library/ws-tip-null/index.html

Community
  • 1
  • 1
finman
  • 545
  • 6
  • 16
  • I mean that when I load the XML into a custom object, the chord property disappears from the object. – kevin Mar 21 '13 at 23:43
  • I think I've got it figured out, thank you for your answer, I've read that all now. Not sure what fixed it. – kevin Mar 21 '13 at 23:46
  • The property will be null if the element is missing, going from XML to a C# object is deserializing, going from C# object to XML is serializing, so you are deserializing, and if the element is missing or empty it won't serialize it. Post your serialization class definition if you have any more questions on how to serialize elements correctly – finman Mar 21 '13 at 23:51