首页 新闻 会员 周边

XML混合内容的反序列化问题

0
悬赏园豆:50 [待解决问题]

对于混合文本的xml:

<remark>He is a <match>tall</match> and handsome man</remark>

目前我用VS的XSD工具生成的类,类似:

[System.Xml.Serialization.XmlElementAttribute("match")]
public match[] match {
  get {
    return this._match;
  }
  set {
    this._match = value;
  }
}

/// 
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text {
  get {
    return this.textField;
  }
  set {
    this.textField = value;
  }
}

但是反序列化后,我无法准确的知道子节点match和文本的顺序。

参考StackOverflow链接:

http://stackoverflow.com/questions/2648202/xmlserializer-mixed-content-deserialization

请求支援。。。

十分师傅的主页 十分师傅 | 初学一级 | 园豆:73
提问于:2015-04-24 12:13
< >
分享
所有回答(2)
0

数组的顺序就是文本的顺序

Launcher | 园豆:45045 (高人七级) | 2015-04-24 13:04

match在文本的中间,我的意思是,反序列化后,文本数组是【He is a,and handsome man】,我要怎么知道match是在两个数组之间的。

支持(0) 反对(0) 十分师傅 | 园豆:73 (初学一级) | 2015-04-24 13:16

@十分师傅: 这个你确实无法知道,放弃吧!

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2015-04-24 13:54
0

添加Items对象集合属性,并添加XmlTextAttribute(不使用Text[])后解决:

[System.Xml.Serialization.XmlElementAttribute("match", typeof(match))]
        [System.Xml.Serialization.XmlTextAttribute(typeof(System.String))] //添加*
        public object[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }

        /* 不使用
        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string[] Text
        {
            get
            {
                return this.textField;
            }
            set
            {
                this.textField = value;
            }
        }
        */
十分师傅 | 园豆:73 (初学一级) | 2015-04-24 15:07

添加一个 match 类后,确实有效果。

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2015-04-24 16:16

@Launcher: 折腾了一个上午哎。我都用VS的xsd工具生成的,看来工具生成的代码也不完美。

支持(0) 反对(0) 十分师傅 | 园豆:73 (初学一级) | 2015-04-24 16:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册