using System.IO; using System.Xml.Serialization; namespace Microsoft.PatchCreationService.Utilities { public static class XmlSerializeHelper { public static void Serialize<T>(string fileName,T target) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (FileStream stream = new FileStream(fileName,FileMode.OpenOrCreate)) { xmlSerializer.Serialize(stream,target); }; } public static T Deserialize<T>(string fileName) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (FileStream stream = new FileStream(fileName,FileMode.OpenOrCreate)) { return (T)xmlSerializer.Deserialize(stream); }; } } }
And if you want to ignore some fields when serializing an object,please put an "[XmlIgnore]" attribute above the fields.
For example:
[
XmlIgnore
]
public
string
NewFilePath;
原文链接:https://www.f2er.com/xml/299884.html