XmlSerializer 不填充子元素
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您定义属性
SubElement
的类是与ParentNodeWhichWorksFine
对应的类?如果是这样,请尝试以下更改:此外,您说您已使用 xsd.exe 生成了此代码。这种情况下的输入是什么 - .xsd 文件?如果是的话,您也可以发布其中的相关部分吗?
I assume the class within which you define property
SubElement
is the one corresponding toParentNodeWhichWorksFine
? If so, try this change: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?
XmlArrayItemAttribute
属性指定由公共成员SubElements
定义的数组元素的子节点的名称。因此,如果该示例 xml 正是生成的 xsd.exe 生成类,则该示例 xml 不符合 xsd。根据生成的类,
项应包含在父
节点中,如下所示:如果您可以控制架构,我认为更改它以使其与示例 xml 相对应(没有父节点,遵循 Pavel 的解决方案),因为父数组节点是多余的。
The
XmlArrayItemAttribute
attribute specifies a name for the child nodes of the array element defined by the public memberSubElements
. 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: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.
看起来生成的类中的 SubElement 数组缺少 [XmlArray] 属性。
它需要看起来像这样:
<代码>
我认为您的 XSD 文件中有些东西不太正确。
Looks like your SubElement array in your generated class is missing the [XmlArray] attribute.
It needs to look like this:
Something is not quite right in your XSD file, I reckon.