c# - 如何使用 XmlSerializer 反序列化未知元素?
问题描述
我有一些需要反序列化的 xml:
<MysteryXml niceAttribute="I know">
<UnknownNode attr1="someValue" attr2="anotherValue"> <- this nodes attributes and children are unknows
<Name>
<Value />
<AnotherValue />
</Name>
<CreatedBy>Unit Test</CreatedBy>
</UnknownNode>
<KnownNode nice="anotherAttr" />
</MysteryXml>
我有一个 C# 类来反序列化 xml
[Serializable()]
[DesignerCategory("code")]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "MysteryXml", IsNullable = false)]
public class MysteryXml
{
[XmlElement]
// Here I have a node[] but I would like an XElement
// If I set the type to XElement I only get the child node
public object? UnknownNode { get; set; }
[XmlElement]
public KnownNode KnownNode { get; set; }
[XmlAttribute]
public string niceAttribute {get;set;}
}
是否可以使用 XmlSerializer 将完整的 xml 作为字符串或 UnknownNode 的 XElement 获取?
谢谢
解决方案
推荐阅读
- yii2 - 将搜索添加到自定义查询 Yii2
- c# - 如何解决 Mono 的问题
- c# - 如何使用多部分/表单数据编码和使用 POST 方法使用带有用户名和密码的 REST API
- html - CSS 文本被覆盖。如何删除文本覆盖?
- wordpress - WordPress 虚拟页面呈现为博客而不是页面
- publish-actions - Facebook“publish_actions”权限
- swift - 从字符串获取子字符串直到字符串索引的结尾
- javascript - 如何调用原始数据重塑过程形成,我们可以轻松访问所需的数据?
- javascript - 如何在不影响应用程序性能的情况下搜索包含字符串数组中指定的任何子字符串的元素?
- python - 替换与数据框中特定字符串匹配的值