反序列化后获取XmlElementAttribute的名称
我还没有找到任何这方面的信息,也许有人可以提供帮助。
我有一个 XML(为方便起见进行了简化):
<content>
<field1>value</field1>
<field2>
<field3>value</field3>
</field2>
</content>
我尝试使用此类类对其进行反序列化:
[XmlRoot("content")]
public class Content
{
[XmlElement]
public List<Item> Fields { get; set; }
}
public class Item
{
[XmlElement]
public List<Item> Fields { get; set; }
[XmlText]
public String Value { get; set; }
}
我有两个问题:
我可以获得字段的实际名称吗?就像 Item 类中的
[XmlName] string name;
一样?或者类本身的某种属性?由于某些原因,不可能将节点名称设置为“field”并添加“type”属性;-) 虽然实际的类和序列化过程确实很复杂,但我不想实现自己的序列化器。我可以添加通配符,例如 [XmlElement("field*")] 吗?在知道第一个问题的答案之前,我无法对其进行测试,因此如果有更好的选择,我也很想知道。
谢谢。
I haven't found any information on this, maybe someone could help.
I have an XML (simplified for convenience):
<content>
<field1>value</field1>
<field2>
<field3>value</field3>
</field2>
</content>
I try to deserialize it using such classes:
[XmlRoot("content")]
public class Content
{
[XmlElement]
public List<Item> Fields { get; set; }
}
public class Item
{
[XmlElement]
public List<Item> Fields { get; set; }
[XmlText]
public String Value { get; set; }
}
I have two questions:
Can I get the actual name of the field? Like
[XmlName] string name;
in the Item class? Or some kind of an attribute for the class itself? It is not possible to set the node name to "field" and add "type" attribute, for some reasons ;-) While the actual class and serialization process is really complicated, I'd prefer not to implement my own serializer.Can I add a wildcard like [XmlElement("field*")]? I can't test it until I know the answer to the first question, so if there is a better option, I'd love to know it as well.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以在Attribute的构造函数中设置匹配的XMl-Element或Attribute的名称
-->将属性序列化/反序列化为 Xml 属性 FieldAsAttribute
或
-->将属性序列化/反序列化为 Xml 元素 FieldAsElement
You can set the name of the matching XMl- Element or Attribute in the Constructor of the Attribute
--> Will Serialize / Deserialize the Property to the Xml Attribute FieldAsAttribute
or
--> Will Serialize / Deserialize the Property to the Xml Element FieldAsElement
不幸的是,这里唯一的答案是这是不可能的。
因此,我们编写了自己的序列化例程。
The only answer here is that it's, unfortunately, not possible.
As a result we have written our own serialization routine.