C# 将XML混合内容中的文本反序列化为自定义对象?
是否可以使用XML(其中一个元素具有混合内容),并将混合元素中的文本反序列化为自定义对象,而不是字符串 我试过这个:C# 将XML混合内容中的文本反序列化为自定义对象?,c#,xml-serialization,C#,Xml Serialization,是否可以使用XML(其中一个元素具有混合内容),并将混合元素中的文本反序列化为自定义对象,而不是字符串 我试过这个: [XmlText(typeof(textType))] [XmlElement("query", typeof(templateBodyQuery))] [XmlElement("expand", typeof(expandType))] [XmlElement("insert", typeof(expandTypeInsert))] pu
[XmlText(typeof(textType))]
[XmlElement("query", typeof(templateBodyQuery))]
[XmlElement("expand", typeof(expandType))]
[XmlElement("insert", typeof(expandTypeInsert))]
public object[] Items { get; set; }
预期文本项将被序列化为textType
,但我得到一个“textType”不能用作“xml文本”的错误
这是我的textType
课程:
public class textType
{
[XmlText]
public string Value { get; set; }
}
不能对XmlText使用非基元类型。此外,我不确定我是否理解xml的结构,因为不能在单个节点下使用XmlText和XmlElements
我想这就是你想要做的:
[XmlElement("textType",typeof(textType))]
[XmlElement("query", typeof(templateBodyQuery))]
[XmlElement("expand", typeof(expandType))]
[XmlElement("insert", typeof(expandTypeInsert))]
public object[] Items { get; set; }
它反序列化:
<Test>
<textType>example</textType>
<query>...</query>
<expand>...</expand>
</Test>
例子
...
...
到一个类Test
,该类在Items
数组的开头有一个textType对象,其值为“example”