XmlSerializer 不填充子元素

发布于 2024-08-09 13:27:28 字数 1016 浏览 3 评论 0原文

我使用 XSD.EXE 将 XSD 转换为对象。这工作得很好,我可以使用 XMLSerializer 进行反序列化,只是作为数组生成的子元素不会填充。

    private SubElements[] subelementsField;

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

即使 XML 中有数据,当我使用以下代码时,它也不会填充它:

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

根元素都工作正常,只是这种类型的 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>

我是否缺少一些提示或其他内容XSD.EXE是不是包含在内?

I've used XSD.EXE to convert an XSD into an object. That works fine and I can Deserialize using XMLSerializer just fine, except that the subelements which are generated as arrays don't populate.

    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:

// 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:

<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不乱于心 2024-08-16 13:27:28

我假设您定义属性 SubElement 的类是与 ParentNodeWhichWorksFine 对应的类?如果是这样,请尝试以下更改:

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

此外,您说您已使用 xsd.exe 生成了此代码。这种情况下的输入是什么 - .xsd 文件?如果是的话,您也可以发布其中的相关部分吗?

I assume the class within which you define property SubElement is the one corresponding to 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. What was the input in that case - an .xsd file? If so, can you post the relevant part of it, too?

陌伤ぢ 2024-08-16 13:27:28

XmlArrayItemAttribute 属性指定由公共成员 SubElements 定义的数组元素的子节点的名称。因此,如果该示例 xml 正是生成的 xsd.exe 生成类,则该示例 xml 不符合 xsd。

根据生成的类, 项应包含在父 节点中,如下所示:

<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>

如果您可以控制架构,我认为更改它以使其与示例 xml 相对应(没有父节点,遵循 Pavel 的解决方案),因为父数组节点是多余的。

The XmlArrayItemAttribute attribute specifies a name for the child nodes of the array element defined by the public member SubElements. So that sample xml doesn't conform to the xsd, if that's the exact generated class xsd.exe produced.

According to the generated class, the <SubElement> items should be contained in a parent <SubElements> node like this:

<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.

小梨窩很甜 2024-08-16 13:27:28

看起来生成的类中的 SubElement 数组缺少 [XmlArray] 属性。

它需要看起来像这样:
<代码>

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

我认为您的 XSD 文件中有些东西不太正确。

Looks like your SubElement array in your generated class is missing the [XmlArray] attribute.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文