简体   繁体   English

将XSD.exe用于c#code gen,忽略XML中的空节点?

[英]Using XSD.exe for c# code gen, ignores empty nodes in XML?

Maybe I'm not doing this correctly. 也许我没有正确地做到这一点。 But I'm using MusicXML , along with XSD.exe to generate the C# classes against the DTD. 但我正在使用MusicXML和XSD.exe来生成针对DTD的C#类。

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> . 根据文档以及示例xml文件 ,如果<NOTE><CHORD>的一部分, <NOTE>元素包含一个空的<CHORD>元素。 The code generated looks like this: 生成的代码如下所示:

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

When I deserialize the XML into ac# object, it works great, but the <CHORD> seems to disappear. 当我将XML反序列化为ac#对象时,它工作得很好,但<CHORD>似乎消失了。 Does .NET just ignore the CHORD element in this sample XML? .NET只是忽略了此示例XML中的CHORD元素吗?

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

Thanks! 谢谢!

Do you mean the chord element dissapears when you serialize to XML, as null elements don't serialize by default 你是否意味着序列化为XML时和弦元素消失,因为默认情况下null元素不会序列化

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# 你可以设置使用isnullable属性XML Serialization和null值 - 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: 如在另一个问题中链接的那样,您可能希望查看有关XML Schema文件中null表示的文章:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM