XML Serialization of List - XML Root

前端 未结 4 1125
说谎
说谎 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 03:57

    There is a much easy way:

    public XmlDocument GetEntityXml()
    {
        XmlDocument xmlDoc = new XmlDocument();
        XPathNavigator nav = xmlDoc.CreateNavigator();
        using (XmlWriter writer = nav.AppendChild())
        {
            XmlSerializer ser = new XmlSerializer(typeof(List), new XmlRootAttribute("TheRootElementName"));
            ser.Serialize(writer, parameters);
        }
        return xmlDoc;
    }
    

提交回复
热议问题