c# xml序列化的一些问题

在序列化时用代码的方式忽略成员

https://stackoverflow.com/questions/18242320/ignore-a-property-during-xml-serialization-but-not-during-deserialization

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attribs = new XmlAttributes();
attribs.XmlIgnore = true;
attribs.XmlElements.Add(new XmlElementAttribute("YourElementName"));
overrides.Add(typeof(YourClass), "YourElementName", attribs);

XmlSerializer ser = new XmlSerializer(typeof(YourClass), overrides);
ser.Serialize(...

防止序列化内存泄露

https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer

static readonly XmlSerializer mySerializer =
    new XmlSerializer(typeof(XMLObj), new XmlRootAttribute("rootNode"))
posted @ 2021-11-18 10:10  trykle  阅读(29)  评论(0)    收藏  举报