XmlSerializer-包含文本+ xml +文本的节点 [英] XmlSerializer - node containing text + xml + text

查看:41
本文介绍了XmlSerializer-包含文本+ xml +文本的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点,可以包含text或xml,text + xml或text + xml + text.我已经解决的前两种情况

I have a node that can contain either text or xml, text + xml or text + xml + text. The first two cases i have solved using

    [XmlAnyElement]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public XmlElement[] TextNodes { get; set; }

    [XmlText]
    public string InnerText { get; set; }

    [XmlIgnore]
    public string Text 
    {
        get
        {
            return String.Format("{0}{1}", InnerText, TextNodes.Aggregate(String.Empty, (current, documentNode) => current + documentNode.OuterXml));
        }
    }

但是,最后一种情况是在xml后面有文本,我找不到解决方案.

However, the last case where there is text after the xml I cant find a solution for.

XML

<node>
 somtext <p>moretext</p> even more text
</node>

我的代码将文本输出为:

my code will give output in Text as:

sometext <p>moretext</p>

我需要:

sometext <p>moretext</p> even more text

推荐答案

您想要的内容称为混合内容,并且在XmlSerializer中受支持.

What you want is called mixed content and is supported in the XmlSerializer.

像这样编写您的课程

[Serializable]
public class TestClass
{
    [XmlText(typeof(string))]
    [XmlAnyElement]
    public object[] Items { get; set; }
}

并将所有元素放在同一对象数组中

and put all the elements in the same object array

这篇关于XmlSerializer-包含文本+ xml +文本的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆