HTML的XML序列化 [英] XML Serialization of HTML

查看:90
本文介绍了HTML的XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好这一个做到了!感谢大家了!

public class Result
{
    public String htmlEscaped
    {
        set;
        get;
    }

    [XmlIgnore]
    public String htmlValue
    { set; get; }

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get
        {
            XmlDocument _dummyDoc = new XmlDocument();
            return _dummyDoc.CreateCDataSection(htmlValue);
        }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}

    Result r = new Result();
    r.htmlValue = ("<b>Hello</b>");
    r.htmlEscaped = ("<b>Hello</b>");
    XmlSerializer xml = new XmlSerializer(r.GetType());
    TextWriter file = new StreamWriter(Environment.CurrentDirectory + "\\results\\result.xml", false, System.Text.Encoding.Default);
    xml.Serialize(file, r);
    file.Close();



结果:

RESULT:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlEscaped>&lt;b&gt;Hello&lt;/b&gt;</htmlEscaped>
  <htmlValue><![CDATA[<b>Hello</b>]]></htmlValue>
</Result>



正如你所看到的,后CDATA是返回类型,在文件系统中的XML文件中没有更多的转义的HTML。
JSON序列是不工作了,但是这可以固定一个小型延伸

As you can see, after CDATA is return type, no more escaped html in XML file on filesystem. The JSON Serialization isn't working anymore, but this can be fixed with a little type extention.



的问题是:


QUESTION WAS:

也许有人知道如何让做...

Maybe someone knows how to make do it...

我有这个类别:

public class Result
{
    public String htmlValue
    {
        get;
        set;
    }
}



我用这个把它序列化到XML

I use this to serialize it to XML

Result res = new Result();
res.htmlValue = "<p>Hello World</p>";
XmlSerializer s = new XmlSerializer(res.GetType());
TextWriter w = new StreamWriter(Environment.CurrentDirectory + "\\result.xml", false, System.Text.Encoding.Default);
s.Serialize(w, res);
w.Close();



工程罚款我得到这样的:

Works fine i get this:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlValue>&lt;b&gt;Hello World&lt;/b&gt;</htmlValue>
</Result>



能做些什么我必须改变,以得到这样的:

What can do i have to change to get this:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>
</Result>



我已经搜查,但我无法找到任何东西。类型htmlValue
的都留字符串,因为其他Serialisations JSON等。

I've already searched but I can't find anything. The type of htmlValue have to stay String, because of other Serialisations JSON, etc.

棘手的一个...预先感谢建议

Tricky one... Thanks in advance for suggestions


  • HTML是正确的字符串C#中。为什么解码编码或?

  • XmlSerializer的保存HTML转义成XML文件。

  • 请不要使用C#进行消费。

时的外部工具,它接受这样的:

Is external tool which accept this:

<htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>



而不是

but not

<htmlValue>&lt;b&gt;Hello World&lt;/b&gt;</htmlValue>



我做的JSON序列相同,文件的硬盘驱动器的HTML保存正确的。
为何以及在何处使用HTTP工具,以防止?以及如何获得<![CDATA []]方式> 周围

你能不能给一个代码样品?
是否有任何其他串行比C#自己一个人?

Can you give a code sample? Are there any other Serializer than the C# own one?

我发现这个链接的从马可·安德烈·席尔瓦的nofollow的>。NET XML序列化,这不会是我需要做的,但它是不同的,如何将这种不改变类型?

I've found this Link .NET XML Serialization of CDATA ATTRIBUTE from Marco André Silva, which does I need to do, but it's different, how to include this without changing Types?

推荐答案

下面是一个简单的技巧做实现你想要的。你只需要序列化XmlCDataSection属性而不是字符串属性:

Here's a simple trick to do achieve what you want. You just need to serialize a XmlCDataSection property instead of the string property :

(这是几乎一样约翰的建议,但是简单一点......)

(it's almost the same as John's suggestion, but a bit simpler...)

public class Result
{
    [XmlIgnore]
    public String htmlValue
    {
        get;
        set;
    }

    private static XmlDocument _dummyDoc;

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get { return _dummyDoc.CreateCDataSection(htmlValue); }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}

这篇关于HTML的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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