反序列化元素的值作为字符串虽然含有混合内容 [英] Deserialize element value as string although it contains mixed content

查看:124
本文介绍了反序列化元素的值作为字符串虽然含有混合内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个像这样的XML:

Assuming an XML like this:

<my:Root xmlns:my="http://foo/bar">
    <my:FieldBasic>content</my:FieldBasic>
    <my:FieldComplex>
        <html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
            <div><h1>content</h1></div>
        </html>
    </my:FieldComplex>
<my:Root>

和一个类,如:

[Serializable]
[XmlType(AnonymousType = true, Namespace = "http://foo/bar")]
[XmlRoot(ElementName = "Root", Namespace = "http://foo/bar", IsNullable = false)]
public class MyRoot
{
    public string FieldBasic { get; set; }
    public string FieldComplex { get; set; }
}



我如何反序列化<我:FieldComplex> ; 来在 FieldComplex 字符串?当它发现里面的HTML它失败。我希望把它给我与此内容的字符串:

How do I deserialize <my:FieldComplex> to a string within FieldComplex? It fails when it finds the HTML inside. I want to make it give me a string with this content:

<html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
    <div><h1>content</h1></div>
</html>

如果我宣布 FieldComplex 公共对象FieldComplex (即的xsd:anyType的),它有点工作,我得到一个的XMLNode [] 里面,我可以使用。

If I declare FieldComplex as public object FieldComplex (i.e. xsd:anyType) it kinda works and I get a XMLNode[] inside which I can use.

但我需要的 FieldComplex 来是字符串类型的序列化作为序列化XML将不包含HTML ,这将是这样的:

But I need the FieldComplex to be of type string for the serialization as for serialization the XML will not contain HTML, it will be like:

<my:Root xmlns:my="http://foo/bar">
    <my:FieldBasic>content</my:FieldBasic>
    <my:FieldComplex>content</my:FieldComplex>
<my:Root>



声明 FieldComplex 的对象将插入这些属性在<我:FieldComplex> 元素:

Declaring FieldComplex as object will insert these attributes on the <my:FieldComplex> element:

 xmlns:q1="http://www.w3.org/2001/XMLSchema" p3:type="q1:string" xmlns:p3="http://www.w3.org/2001/XMLSchema-instance

和我不希望这样。我也不想使用序列化和反序列化不同的类。

and I don't want that. I also don't want to use different classes for serialization and deserialization.

那么,这可能吗?

为了使长话短说,是有可能有这个类:

To make a long story short, is it possible to have this class:

public class MyRoot
{
    public string FieldBasic { get; set; }
    public string FielComplex { get; set; }
}

序列化到这一点:

<my:Root xmlns:my="http://foo/bar">
    <my:FieldBasic>content</my:FieldBasic>
    <my:FieldComplex>content</my:FieldComplex>
<my:Root>

和从该反序列化:

<my:Root xmlns:my="http://foo/bar">
    <my:FieldBasic>content</my:FieldBasic>
    <my:FieldComplex>
        <html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
            <div><h1>content</h1></div>
        </html>
    </my:FieldComplex>
<my:Root>



?

PS 只是为了解释的为什么。我有一个类巫婆被序列化。然后,序列化的XML通过在应用程序中多个节点传播,并最终回来了,但改变了像上面。该层做一些XML验证,并输入有额外的属性或元素失败,验证并停止流动。我要地图返回XML同一类。内容主要是来自的看法,但当然不一样的序列化/反序列化它的观点只是字符串:(

P.S. Just to explain "the why?". I have a class witch gets serialized. The serialized XML then travels through multiple nodes in the application and eventually comes back but altered like above. The layers do some XML validation and having extra attributes or elements on input fail the validation and stops the flow. I want to map the return XML to the same class. The content is just strings from it's point of view but of course not the same for serialization/deserialization :(

推荐答案

这ISN ŧ完全结束,因为我不记得,如果你能/如何命名空间前缀添加到XML序列化的根元素但是,如果你实现IXmlSerializable接口在MyRoot类这样的:

This isn't quite finished because I can't remember if you can / how to add the namespace prefix to the root element in Xml Serialization. But if you implement the IXmlSerializable interface in your MyRoot class like this:

[XmlRoot("Root", Namespace="http://foo/bar")]
public class MyRoot : IXmlSerializable

然后自己编写XML序列化方法,像这样:

Then write the XML serialization methods yourself, something like this:

        void IXmlSerializable.ReadXml(System.Xml.XmlReader reader)
        {
            reader.MoveToContent();
            var outerXml = reader.ReadOuterXml();
            XElement root = XElement.Parse(outerXml);

            this.FieldBasic = root.Elements(XName.Get("FieldBasic", "http://foo/bar")).First().Value;
            this.FieldComplex = root.Elements(XName.Get("FieldComplex", "http://foo/bar")).First().Elements().First().Value.Trim();
        }



        void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteRaw(String.Format("\r\n\t<my:FieldBasic>\r\n\t\t{0}\r\n\t</my:FieldBasic>", this.FieldBasic));
            writer.WriteRaw(String.Format("\r\n\t<my:FieldComplex>\r\n\t\t{0}\r\n\t</my:FieldComplex>\r\n", this.FieldComplex));
        }



(从GetSchema方法返回null)

(Return null from the GetSchema method)

这应该让你至少非常接近你追求的。

This should get you at least pretty close to what you're after.

您也可以发现这些链接有帮助的。

You may also find these links helpful.

的IXmlSerializable

命名空间

这篇关于反序列化元素的值作为字符串虽然含有混合内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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