xml 序列化时如何将某个字符串成员写成CDATA块
2008-05-06 14:36
507 查看
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><bodystr
4000
ing><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; }
}
}
}
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><bodystr
4000
ing><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; }
}
}
}
相关文章推荐
- Json.NET 序列化JSON字符串时,如何忽略值为null的成员?
- [Unity 笔记] unity中如何将Object序列化成xml字符串并保存
- 如何将字符串序列化到CDATA块中(Serializing A String Within a CDATA Element )
- XML如何序列化成字符串
- 如何从XML字符串获取DataSet
- 如何在XML字符串中使用&
- Android 如何解析Xml字符串
- 【C#】解决进行反序列化时出错:。InnerException 消息是“反序列化对象 属于类型 System.String 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。
- 如何在Android的xml中添加带<<>>的字符串
- C#中如何读取webconfig.xml里的数据库连接字符串
- 如何使用JS脚本处理XML字符串或XML文件(适用于IE、Opera、Firefox浏览器)
- xml反序列化时,如何生成与之对应的类文件
- 如何将一个XML的字符串加载到DataSet中
- 如何从XML字符串获取DataSet
- 如何从XML字符串获取DataSet
- 如何从XML字符串获取DataSet
- hibernate中写配置文件xxx.hbm.xml时那个成员变量的类型type如何写的问题
- 如何格式化字符串的xml?
- C#2005如何读取XML文件中的数据库连接字符串??? (好像和2003不一样啊) 急,跪求!
- java如何解析传来的xml字符串