简体   繁体   English

XmlSerializer不填充子元素

[英]XmlSerializer not populating sub-elements

I've used XSD.EXE to convert an XSD into an object. 我已经使用XSD.EXE将XSD转换为对象。 That works fine and I can Deserialize using XMLSerializer just fine, except that the subelements which are generated as arrays don't populate. 效果很好,我可以使用XMLSerializer进行反序列化,只是不会填充作为数组生成的子元素。

    private SubElements[] subelementsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("SubElement", IsNullable=false)]
    public SubElement[] SubElement {
        get {
            return this.subelementField;
        }
        set {
            this.subelementField = value;
        }
    }

Even though there is data in the XML it just doesn't populate it when I use the following code: 即使XML中有数据,当我使用以下代码时,它也不会填充它:

// Deserialize
var result = serializer.Deserialize(new StringReader(data.XMLText.ToString()));

The root elements all work fine, just not the sub elements of this type of XML data: 根元素都可以正常工作,而不能兼容这种XML数据的子元素:

<RootNode Weight="205" Year="1995">
  <ParentNodeWhichWorksFine Contact="John Doe">
    <SubElement SomeAttribute="123">
      <Location>New York City</Location>
      <Team>New York Pizza</Team>
    </SubElement>
  </ParentNodeWhichWorksFine>
</RootNode>

Am I missing some hints or something else that XSD.EXE is not including? 我是否缺少一些提示或XSD.EXE不包括的其他内容?

I assume the class within which you define property SubElement is the one corresponding to ParentNodeWhichWorksFine ? 我假设您在其中定义属性SubElement是与ParentNodeWhichWorksFine相对应的ParentNodeWhichWorksFine If so, try this change: 如果是这样,请尝试以下更改:

[XmlElement("SubElement", IsNullable=false)]
public SubElement[] SubElement

Also, you say that you've generated this code using xsd.exe. 另外,您说您已经使用xsd.exe生成了此代码。 What was the input in that case - an .xsd file? 在这种情况下,输入什么-.xsd文件? If so, can you post the relevant part of it, too? 如果是这样,您也可以张贴其中的相关部分吗?

The XmlArrayItemAttribute attribute specifies a name for the child nodes of the array element defined by the public member SubElements . XmlArrayItemAttribute属性为公共成员SubElements定义的数组元素的子节点指定名称。 So that sample xml doesn't conform to the xsd, if that's the exact generated class xsd.exe produced. 因此,如果生成的正是xsd.exe类,则该示例xml不符合xsd。

According to the generated class, the <SubElement> items should be contained in a parent <SubElements> node like this: 根据生成的类, <SubElement>项应包含在父<SubElements>节点中,如下所示:

<RootNode Weight="205" Year="1995">
  <ParentNodeWhichWorksFine Contact="John Doe">
    <SubElements>
      <SubElement SomeAttribute="123">
        <Location>New York City</Location>
        <Team>New York Pizza</Team>
      </SubElement>
    </SubElements>
  </ParentNodeWhichWorksFine>
</RootNode>

If you have control over the schema, I think changing it so that it corresponds to the sample xml is preferable (no parent node, following Pavel's solution ), since the parent array nodes are superfluous. 如果您对模式有控制权,我认为最好更改它,使其与示例xml相对应(不使用Pavel解决方案的父节点),因为父数组节点是多余的。

Looks like your SubElement array in your generated class is missing the [XmlArray] attribute. 看起来您生成的类中的SubElement数组缺少[XmlArray]属性。

It needs to look like this: 它需要看起来像这样:


[System.Xml.Serialization.XmlArrayAttribute("SubElements")]
[System.Xml.Serialization.XmlArrayItemAttribute("SubElement", IsNullable=false)]
public SubElement[] SubElement {
}

Something is not quite right in your XSD file, I reckon. 我认为您的XSD文件中有些不正确。

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

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