开发者

C# Deserialize text in XML mixed content as custom object?

Is it possible to have an XML, one of which elements has mixed content, and deserialize the text in the mixed element as a custom object instead of as string?

I tried this:

    [XmlText(typeof(textType))]
    [XmlElement("query", typeof(templateBodyQuery))]
    [XmlElement("expand", typeof(expandType))]
    [XmlElement("insert", typeof(expandTypeInsert))]
    public object[] Items { get; set; }

Expecting the text items would be serialized as textType, but I get an 'textType' cannot be used as 'xml text' error.

This is my textType class:

public class textType
{
    [Xm开发者_JS百科lText]
    public string Value { get; set; }
}


You can't use non primitive types for XmlText. Also I'm not sure I understand how that xml would be structured as you can't have XmlText and XmlElements under a single node.

I think this is what you're trying to do:

[XmlElement("textType",typeof(textType))]
[XmlElement("query", typeof(templateBodyQuery))]
[XmlElement("expand", typeof(expandType))]
[XmlElement("insert", typeof(expandTypeInsert))]
public object[] Items { get; set; }

which deserializes:

<Test>
    <textType>example</textType>
    <query>...</query>
    <expand>...</expand>
</Test>

To a class Test that has a textType object at the start of the Items array with Value of "example"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜