不XmlArray使用XmlArrayItem属性的序列化的C#类

问题描述:

我要在以下格式的XML:不XmlArray使用XmlArrayItem属性的序列化的C#类

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

我试图创建一个类Configuration具有[Serializable]属性。序列化凭证的节点,我有以下几点:

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

然而,当我序列这对XML的XML是按以下格式:

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

如果我删除了[XmlArray("configuration")]行,我得到如下:

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

我如何序列化此我想要的方式,与多个<credentials>节点的单个根节点<configuration>内?我想这样做,而不必执行IXmlSerializable并执行自定义序列化。这是我的班级的描述:

[Serializable] 
[XmlRoot("configuration")] 
public class Configuration : IEquatable<Configuration> 

以下内容应该按照您想要的方式正确序列化。列表上的线索是[XmlElement("credentials")]。我通过使用xml来实现这一点,在Visual Studio中从它生成一个模式(xsd)。然后在模式上运行xsd.exe以生成一个类。 (和一些小的修改)

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(); 
    } 
} 

给下面的输出

<?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> 
+2

是的,这工作!我是这样设置的“它是一个List”,必须使用'XmlArrayItem'属性“,我甚至没有想到将它定义为一个常规的'XmlElement'。 – 2010-07-21 20:22:45

+2

很好的答案,我有同样的问题 - 我想删除: DATAITEMS> 与解决它: [的XmlElement( “DataItem的”) 公开名单的DataItem {获得;组; } – nologo 2011-06-23 11:04:31

+0

感谢Mikael。这帮助我解决了今天的问题。 +1 – John 2011-10-31 19:10:28