XML Serialization of List - XML Root

前端 未结 4 1123
说谎
说谎 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

    so simple....

    public static XElement ToXML(this IList lstToConvert, Func filter, string rootName)
    {
        var lstConvert = (filter == null) ? lstToConvert : lstToConvert.Where(filter);
        return new XElement(rootName,
           (from node in lstConvert
           select new XElement(typeof(T).ToString(),
           from subnode in node.GetType().GetProperties()
           select new XElement(subnode.Name, subnode.GetValue(node, null)))));
    
    }
    

提交回复
热议问题