List的 XML 序列化 - XML 根

发布于 2024-07-30 02:33:15 字数 1322 浏览 3 评论 0原文

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

所以我尝试返回一个列表的 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;
    }

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

假设我想获取 List 的 XML,

该 XML 类似于:

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

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

示例:

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

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

First question on Stackoverflow (.Net 2.0):

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

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>

The XML would be something like:

<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?

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滥情空心 2024-08-06 02:33:15

有一个非常简单的方法:

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;
}

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;
}
心安伴我暖 2024-08-06 02:33:15

如果我理解正确,您希望文档的根始终相同,无论集合中元素的类型是什么? 在这种情况下,您可以使用 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);

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<T>), attr);
       XmlSerializer serializer = new XmlSerializer(typeof(List<T>), overrides);
       List<T> parameters = GetAll();
       serializer.Serialize(xmlWriter, parameters);
随梦而飞# 2024-08-06 02:33:15

实现同一件事的更好方法:

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;
}

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;
}
残疾 2024-08-06 02:33:15

很简单....

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)))));

}

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)))));

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文