C# List<T> 的 XML 序列化 - XML 根

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1237683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:02:41  来源:igfitidea点击:

XML Serialization of List<T> - XML Root

c#xmlxml-serializationrootgeneric-list

提问by Matthew Grima

First question on Stackoverflow (.Net 2.0):

关于 Stackoverflow (.Net 2.0) 的第一个问题:

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

所以我试图用以下内容返回一个列表的 XML:

public XmlDocument GetEntityXml()
    {        
        StringWriter stringWriter = new StringWriter();
        XmlDocument xmlDoc = new XmlDocument();            

        XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

        XmlSerializer serializer = new XmlSerializer(typeof(List<T>));

        List<T> parameters = GetAll();

        serializer.Serialize(xmlWriter, parameters);

        string xmlResult = stringWriter.ToString();

        xmlDoc.LoadXml(xmlResult);

        return xmlDoc;
    }

Now this will be used for multiple Entities I have already defined.

现在这将用于我已经定义的多个实体。

Say I would like to get an XML of List<Cat>

说我想得到一个 XML List<Cat>

The XML would be something like:

XML 将类似于:

<ArrayOfCat>
  <Cat>
    <Name>Tom</Name>
    <Age>2</Age>
  </Cat>
  <Cat>
    <Name>Bob</Name>
    <Age>3</Age>
  </Cat>
</ArrayOfCat>

Is there a way for me to get the same Root all the time when getting these Entities?

有没有办法让我在获取这些实体时始终获得相同的 Root?

Example:

例子:

<Entity>
  <Cat>
    <Name>Tom</Name>
    <Age>2</Age>
  </Cat>
  <Cat>
    <Name>Bob</Name>
    <Age>3</Age>
  </Cat>
</Entity>

Also note that I do not intend to Deserialize the XML back to List<Cat>

另请注意,我不打算将 XML 反序列化回 List<Cat>

采纳答案by Suminder

There is a much easy way:

有一个非常简单的方法:

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

回答by Thomas Levesque

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 :

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

回答by John Saunders

A better way to the same thing:

更好的方法来做同样的事情:

public XmlDocument GetEntityXml<T>()
{
    XmlAttributeOverrides overrides = new XmlAttributeOverrides();
    XmlAttributes attr = new XmlAttributes();
    attr.XmlRoot = new XmlRootAttribute("TheRootElementName");
    overrides.Add(typeof(List<T>), attr);

    XmlDocument xmlDoc = new XmlDocument();
    XPathNavigator nav = xmlDoc.CreateNavigator();
    using (XmlWriter writer = nav.AppendChild())
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<T>), overrides);
        List<T> parameters = GetAll<T>();
        ser.Serialize(writer, parameters);
    }
    return xmlDoc;
}

回答by abdul kader jeelani

so simple....

很简单....

public static XElement ToXML<T>(this IList<T> lstToConvert, Func<T, bool> 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)))));

}