序列化为xml时,未使用我的嵌套类集合XMLRoot名称

时间:2016-05-24 13:22:17

标签: c# xml

我有一个填充的模型,我希望加载到xml文档。

由于命名约定,我必须克服我的XML文档的类名,

这是我的模型:

[Serializable]
[XmlRoot("preferences")]
public class PreferencesModel
{
    [XmlIgnore]
    public string MessageToUser { get; set; }

    [XmlElement(ElementName = "sectiondivider")]
    public List<SectionDivider> SectionDivider { get; set; }
}

[Serializable]
[XmlRoot(ElementName = "sectiondivider")]  
public class SectionDivider
{
    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlElement("preference")]
    public List<PreferenceModel> PreferenceModel { get; set; }
}

[Serializable]
[XmlRoot("preference")]
public class PreferenceModel
{

    [XmlAttribute("type")]
    public string Type { get; set; }

    public string Name { get; set; }

    [XmlAttribute("value")]
    public string Value { get; set; }       

    [XmlElement("options")]
    public List<Option> Options { get; set; }
}

这是我序列化的方式:

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(ObjectToXmlString(obj, includeNameSpace, includeStartDocument, rootAttribute));
return xDoc;

public static string ObjectToXmlString(Object obj, bool includeNameSpace, bool includeStartDocument, XmlRootAttribute rootAttribute)
    {
        SpecialXmlWriter stWriter = null;
        XmlSerializer xmlSerializer = default(XmlSerializer);
        string buffer = null;
        try
        {
            if (rootAttribute == null)
            {
                xmlSerializer = new XmlSerializer(obj.GetType());
            }
            else
            {
                xmlSerializer = new XmlSerializer(obj.GetType(), rootAttribute);
            }
            MemoryStream memStream = new MemoryStream();

            StringWriter writer = new StringWriter();

            stWriter = new SpecialXmlWriter(memStream, new UTF8Encoding(false), includeStartDocument);

            if (!includeNameSpace)
            {
                System.Xml.Serialization.XmlSerializerNamespaces xs = new XmlSerializerNamespaces();
                //To remove namespace and any other inline 
                //information tag                      
                xs.Add("", "");
                xmlSerializer.Serialize(stWriter, obj, xs);
            }
            else
            {
                xmlSerializer.Serialize(stWriter, obj);
            }
            buffer = Encoding.UTF8.GetString(memStream.ToArray());    
        }
        catch (Exception e)
        {
            string msg = e.Message;
            throw;
        }
        finally
        {
            if (stWriter != null)
                stWriter.Close();
        }
        return buffer;
    }

我称之为:

XmlDocument preferencesxml = Codec.ObjectToXml(m.SectionDivider,false,
         false, new XmlRootAttribute("preferences"));

我的值是:

enter image description here

我生成的XML是:

enter image description here

1 个答案:

答案 0 :(得分:2)

XmlRootAttribute,顾名思义,仅适用于被序列化的XML的根元素。

您需要在此上下文中使用XmlTypeAttribute

[XmlType("sectiondivider")]`
public class SectionDivider
{
    //...
}

另外,[Serializable]属性与XmlSerializer无关 - 除非您出于其他目的需要,否则可以删除它。