xml 序列化时如何将某个字符串成员写成CDATA块

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Xml.Serialization;

using System.IO;

namespace ConsoleApplication4

{

class Program

{

static MyClass _first;

static MyClass _second;

static string _sentence;

static string _xmlSentence="<?xml version=/"1.0/" encoding=/"utf-8/"?><RequestEnvelope ><header><stringState name=/"locale/" value=/"en-US/" /><stringState name=/"logCorrelationID/" value=/"pni3w455.73430.01.ghorpade.00006/" /><integerState name=/"timezone/" value=/"19800000/" /><booleanState name=/"allowPaging/" value=/"true/" /></header><body><bodystring><b>";

static void Main(string[] args)

{

_first = new MyClass();

_first._field3 = _xmlSentence;

_first.Field0 = "USA";

_first.Field1 = "England";

_sentence=Write();

Console.WriteLine(_sentence);

Console.WriteLine(Read(_sentence));

Console.Read();

}

public static string Write()

{

XmlSerializer xs = new XmlSerializer(typeof(MyClass));

StringWriter sw = new StringWriter();

xs.Serialize(sw,_first);

return sw.ToString();

}

public static string Read(string phrase)

{

XmlSerializer xs = new XmlSerializer(typeof(MyClass));

if (xs.CanDeserialize(new XmlTextReader(new StringReader(phrase))))

{

StringReader sr = new StringReader(phrase);

_second = xs.Deserialize(sr) as MyClass;

return _second._field3;

}

else

return null;

 

 

}

}

[Serializable]

public class MyClass

{

public MyClass()

{

this._field4 = new List<string>();

for(int i=65;i<69;i++)

this._field4.Add(((char)i).ToString());

}

private string _field0;

public string Field0

{

get { return _field0; }

set { _field0 = value; }

}

private string _field1;

[XmlAttribute]

public string Field1

{

get { return _field1; }

set { _field1 = value; }

}

[XmlIgnore]

public string _field3;

public XmlCDataSection Field3

{

get

{

XmlDataDocument doc = new XmlDataDocument();

XmlCDataSection cd = doc.CreateCDataSection(this._field3);

return cd;

}

set

{

this._field3 = value.Value;

}

 

}

private List<string> _field4;

public List<string> Field4

{

get { return _field4; }

set { _field4 = value; }

}


}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值