Using XmlArrayItem attribute without XmlArray in Serializable C # class

I need XML in the following format:

<configuration><!-- Only one configuration node --> <logging>...</logging><!-- Only one logging node --> <credentials>...</credentials><!-- One or more credentials nodes --> <credentials>...</credentials> </configuration> 

I am trying to create a Configuration class that has the [Serializable] attribute. For serializing credential nodes, I have the following:

 [XmlArray("configuration")] [XmlArrayItem("credentials", typeof(CredentialsSection))] public List<CredentialsSection> Credentials { get; set; } 

However, when I serialize this to XML, XML has the following format:

 <configuration> <logging>...</logging> <configuration><!-- Don't want credentials nodes nested in a second configuration node --> <credentials>...</credentials> <credentials>...</credentials> </configuration> </configuration> 

If I delete the line [XmlArray("configuration")] , I get the following:

 <configuration> <logging>...</logging> <Credentials><!-- Don't want credentials nodes nested in Credentials node --> <credentials>...</credentials> <credentials>...</credentials> </Credentials> </configuration> 

How can I serialize this the way I want with multiple <credentials> nodes within the same root node <configuration> ? I wanted to do this without having to implement IXmlSerializable and do custom serialization. This is how my class is described:

 [Serializable] [XmlRoot("configuration")] public class Configuration : IEquatable<Configuration> 
+43
c # xml serializable xml-serialization xml-deserialization
Jul 21 '10 at 19:44
source share
1 answer

The following should be properly ordered the way you want. Hint [XmlElement("credentials")] in the list. I did this by taking your xml, creating a diagram (xsd) in Visual Studio. Then run xsd.exe in the schema to create the class. (And some small changes)

 public class CredentialsSection { public string Username { get; set; } public string Password { get; set; } } [XmlRoot(Namespace = "", IsNullable = false)] public class configuration { /// <remarks/> public string logging { get; set; } /// <remarks/> [XmlElement("credentials")] public List<CredentialsSection> credentials { get; set; } public string Serialize() { var credentialsSection = new CredentialsSection {Username = "a", Password = "b"}; this.credentials = new List<CredentialsSection> {credentialsSection, credentialsSection}; this.logging = "log this"; XmlSerializer s = new XmlSerializer(this.GetType()); StringBuilder sb = new StringBuilder(); TextWriter w = new StringWriter(sb); s.Serialize(w, this); w.Flush(); return sb.ToString(); } } 

enter the following output

 <?xml version="1.0" encoding="utf-16"?> <configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <logging>log this</logging> <credentials> <Username>a</Username> <Password>b</Password> </credentials> <credentials> <Username>a</Username> <Password>b</Password> </credentials> </configuration> 
+66
Jul 21 '10 at 20:15
source share



All Articles