As you can see, comments are not allowed in serialized XML, but this should not be a problem for you. You cannot control the source XML, but you control the deserialization process, so just delete all comments before deserialization:
XmlSerializer xmlSerializer = new XmlSerializer(typeof(myobject)); // load document XmlDocument doc = new XmlDocument(); doc.Load(filename); // remove all comments XmlNodeList l = doc.SelectNodes("//comment()"); foreach (XmlNode node in l) node.ParentNode.RemoveChild(node); // store to memory stream and rewind MemoryStream ms = new MemoryStream(); doc.Save(ms); ms.Seek(0, SeekOrigin.Begin); // deserialize using clean xml xmlSerializer.Deserialize(XmlReader.Create(ms));
If your objects are huge and you deserialize a huge number of them in a short amount of time, howler, we can explore some of Xpath's extracurricular fast readers.
source share