好吧,这一个做到了! 感谢大家!
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();
结果:
<?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><b>Hello</b></htmlEscaped>
<htmlValue><![CDATA[<b>Hello</b>]]></htmlValue>
</Result>
正如你所看到的,后CDATA是返回类型,在文件系统上的XML文件中没有更多的转义HTML。 JSON序列是不工作了,但是这可以固定一个小型扩展。
问题是:
也许有人知道如何让做...
我有这个类:
public class Result
{
public String htmlValue
{
get;
set;
}
}
我用这个把它序列化到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();
做工精细,我得到这样的:
<?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><b>Hello World</b></htmlValue>
</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">
<htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>
</Result>
我已经搜查,但我无法找到任何东西。 该类型htmlValue的都留字符串,因为其他Serialisations JSON等。
棘手的一个......提前建议谢谢
- HTML是在C#中的字符串正确。 为什么解码或编码?
- XmlSerializer的保存HTML转义成XML文件。
- 不要使用C#进行消费。
是接受这个外部工具:
<htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>
但不是
<htmlValue><b>Hello World</b></htmlValue>
我做同样的JSON序列,在硬盘上的HTML保存正确的文件。 为什么何地使用HTTP工具,以防止? 而如何才能获得<![CDATA[ ]]>
周围。
你能不能给一个代码示例? 是否有任何其他串行比C#自己一个人?
我发现这个链接CDATA属性的.NET XML序列化从马可·安德烈·席尔瓦,这不会是我需要做的,但它是不同的,如何将之纳入不改变类型?