用 XmlSerializer的Serialize方法 序列化类 当类中包含 html代码时 经常但不是每次会发生XML标记溢出
用 XmlSerializer的Serialize方法 序列化类 当类中包含 html代码时 经常但不是每次会发生XML标记溢出
我的代码:
XmlSerializer xs = new XmlSerializer(typeof(BaseConfig));
FileStream fs = new FileStream(Handler.MapPath("/base.config"), FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
using (StreamWriter myWriter = new StreamWriter(fs))
{
xs.Serialize(myWriter, config);
myWriter.Close();
}
fs.Close();
序列化过程中无任何错误,但是打开XML文件查看内容有错,如
<?xml version="1.0" encoding="utf-8"?>
<BaseConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FileEncode>utf-8</FileEncode>
<HomeBottom><p style="text-align:center; padding:0 0 2px 0;">
<a href="#" target="_blank"><img alt="" src="/images/msn.jpg" style="width: 74px; height: 22px; " /></a></p>
<p style="text-align:center; padding:0px 0 0px 0;">
<a href="#" target="_blank"><img alt="" src="/images/xl.jpg" style="width: 74px; height: 22px; " /></a></p></HomeBottom>
<ImageExt>jpge,gif,jpg,png</ImageExt>
<ImagePath>/File/</ImagePath>
<MaxUpFileSize>2048</MaxUpFileSize>
<SiteName>Wolfberry health experts</SiteName>
<Title>Wolfberry health experts</Title>
<Url>#</Url>
</BaseConfig>fberry health experts</Title>
<Url>#</Url>
</BaseConfig>
多了下面这个标记
fberry health experts</Title>
<Url>#</Url>
</BaseConfig>
发生了溢出,导致反序列化出错,手动删除多余标记,反序列化正常
类中已指示可序列化
namespace Test.Model
{
[Serializable]
public class BaseConfig
{
...
网上说,XmlSerializer的Serialize方法会自动转换非打印字符,<>括号也已自动转换
请问我应怎么改?
------解决方案--------------------
你是要覆盖不是追加吧
FileStream fs = new FileStream(Handler.MapPath("/base.config"),
FileMode.Create, FileAccess.Write, FileShare.Write);
参数改成这样