[英]How to prevent object to be serialize in XML
IDE:VS,C#.net 4.0,winforms
借助于XMLSerializer,我能够生成C#对象的XML,但是我想删除特定的对象。
有什么方法可以防止在XML ..中阻止特定对象?
using (MemoryStream xmlStream = new MemoryStream())
{
/*
XmlSerializer.Serialize Method (XmlWriter, Object)
Serializes the specified Object and writes the XML document to a file using the specified xmlwriter
Parameters
xmlWriter-
Type: System.Xml.XmlWriter
The XmlWriter used to write the XML document.
Type: System.Object
The Object to serialize.
*/
xmlSerializer.Serialize(xmlStream, YourClassObject);
xmlStream.Position = 0;
//Loads the XML document from the specified string.
xmlDoc.Load(xmlStream);
string fileName = YourClassObject.GetType().Name;
xmlDoc.Save("C:\\Users\\Yogesh\\Desktop\\Yardz_XMLS\\" + fileName + ".xml");
return xmlDoc.InnerXml;
有什么办法可以防止某些属性被序列化吗?
将XmlIgnore
属性放在不需要序列化的属性上:
public class YourClass
{
public string Serailized { get; set; }
[XmlIgnore]
public string NotSerialized { get; set; }
}
有关更多信息,请参见“ 使用属性控制XML序列化 ”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.