public class XMLData
{
public string Data1 { get; set; }
public string Data2 { get; set; }
}
private void Deserialize()
{
try
{
string strXML = string.Empty;
strXML = "a a b c ";
string strTemp = "" + strXML;
XmlRootAttribute rt = new XmlRootAttribute("Roots");
XmlSerializer serializer = new XmlSerializer(typeof(XMLData[]), rt);
XMLData[] xmlDatas = null;
byte[] bt = Encoding.UTF8.GetBytes(strTemp);
MemoryStream ms = new MemoryStream(bt);
xmlDatas = (XMLData[])serializer.Deserialize(ms);
}
catch (Exception ex)
{
}
}
private void Serialize(XMLData[] xmlDatas)
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(XMLData[]));
StringBuilder stringBuilder = new StringBuilder();
System.Xml.XmlWriterSettings xWriSetting = new System.Xml.XmlWriterSettings();
xWriSetting.Encoding = Encoding.UTF8;
xWriSetting.ConformanceLevel = System.Xml.ConformanceLevel.Document;
xWriSetting.CheckCharacters = false;
xWriSetting.Indent = false;
xWriSetting.OmitXmlDeclaration = true;
xWriSetting.CloseOutput = false;
System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(stringBuilder, xWriSetting);
serializer.Serialize(xmlWriter, xmlDatas);
string strOp = stringBuilder.ToString();
}
catch (Exception ex)
{
}
}
No comments:
Post a Comment