XmlSerializer List Item Element Name

Mark your class with the following attributes: [XmlType(“Account”)] [XmlRoot(“Account”)] The XmlType attribute will result in the output requested in the OP. Per the documentation: Controls the XML schema that is generated when the attribute target is serialized by the XmlSerializer

Deserialize object property with StringReader vs XmlNodeReader

It looks like this is a very old XmlNodeReader bug that Microsoft have no intention of fixing. (Archived Microsoft Connect link here). I found a workaround on Lev Gimelfarb’s blog here that adds namespaces to the reader’s NameTable as prefixes are looked up. public class ProperXmlNodeReader : XmlNodeReader { public ProperXmlNodeReader(XmlNode node) : base(node) { … Read more

WCF Error “Maximum number of items that can be serialized or deserialized in an object graph is ‘65536’”

Configuring the below values solved the problem for me. Client Config: <system.serviceModel> <bindings> <basicHttpBinding> <binding name=”BasicHttpBinding_IManagementService” closeTimeout=”00:01:00″ openTimeout=”00:01:00″ receiveTimeout=”00:10:00″ sendTimeout=”00:01:00″ allowCookies=”false” bypassProxyOnLocal=”false” hostNameComparisonMode=”StrongWildcard” maxBufferSize=”2147483647″ maxBufferPoolSize=”524288″ maxReceivedMessageSize=”2147483647″ messageEncoding=”Text” textEncoding=”utf-8″ transferMode=”Buffered” useDefaultWebProxy=”true”> <readerQuotas maxDepth=”128″ maxStringContentLength=”2147483647″ maxArrayLength=”16384″ maxBytesPerRead=”4096″ maxNameTableCharCount=”16384″ /> <security mode=”None”> <transport clientCredentialType=”None” proxyCredentialType=”None” realm=”” /> <message clientCredentialType=”UserName” algorithmSuite=”Default” /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address=”http://XXXX/ManagementService.svc” … Read more

FileMode.Open and FileMode.OpenOrCreate difference when file exists? c# bug?

I have just reproduced that issue. As I wrote in comment. FileMode.Open erases contents of the file while FileMode.OpenOrCreate does not. It seems that new content of the file is one char shorter than previous that’s why you see “>” at the end. If you are writing the file use FileMode.Create that should do for … Read more

How to write a comment to an XML file when using the XmlSerializer?

This is possible using the default infrastructure by making use of properties that return an object of type XmlComment and marking those properties with [XmlAnyElement(“SomeUniquePropertyName”)]. I.e. if you add a property to Foo like this: public class Foo { [XmlAnyElement(“VersionComment”)] public XmlComment VersionComment { get { return new XmlDocument().CreateComment(“The application version, NOT the file version!”); … Read more

XmlSerializer serialize generic List of interface

You can use XmlSerializer as well, but you need to include all the possible types that can appear in the object graph you’re serializing, which limits extensibility and lowers maintainability. You can do it by using an overload of the constructor of XmlSerializer: var x = new XmlSerializer(animals.GetType(), new Type[] { typeof(Cat), typeof(Dog) }); Also, … Read more