XML Serialization of List - XML Root

前端 未结 4 1116
说谎
说谎 2020-12-05 03:42

First question on Stackoverflow (.Net 2.0):

So I am trying to return an XML of a List with the following:

public XmlDocument GetEntityXml()
    {             


        
4条回答
  •  旧巷少年郎
    2020-12-05 04:07

    If I understand correctly, you want the root of the document to always be the same, whatever the type of element in the collection ? In that case you can use XmlAttributeOverrides :

           XmlAttributeOverrides overrides = new XmlAttributeOverrides();
           XmlAttributes attr = new XmlAttributes();
           attr.XmlRoot = new XmlRootAttribute("TheRootElementName");
           overrides.Add(typeof(List), attr);
           XmlSerializer serializer = new XmlSerializer(typeof(List), overrides);
           List parameters = GetAll();
           serializer.Serialize(xmlWriter, parameters);
    

提交回复
热议问题