【问题标题】:save XML list with c#用 c# 保存 XML 列表
【发布时间】:2020-04-21 15:54:09
【问题描述】:
 var xusabugun = root.Elements().Where(s => s.Element("geoId").Value == "US"
                  && DateTime.Parse(s.Element("dateRep").Value).Date == DateTime.Now.Date);

我想将此代码保存为 xml 文件。如果那些可以提供帮助的人,我将不胜感激。从现在开始谢谢你!

【问题讨论】:

  • 你想把结果保存到xml文件吗?你用过XDocument吗?
  • @sajid 是的,我想将结果保存在 xml 文件中。我从互联网上收到数据。
  • 您的查询结果是一个数组。因此,您可以使用根创建一个新文档。然后将数组添加到根。最后保存新文档。
  • @jdweng 是的,这是一个列表,因此我无法注册。你能写出让我注册的代码吗?

标签: c# xml c#-4.0 c#-3.0 c#-2.0


【解决方案1】:

尝试以下:

            XDocument doc = XDocument.Load("filename");
            XElement root = doc.Root;
            List<XElement> xusabugun = root.Elements()
                .Where(s => s.Element("geoId").Value == "US" && DateTime.Parse(s.Element("dateRep").Value).Date == DateTime.Now.Date)
                .ToList();

            XDocument newDoc = XDocument.Parse("<root></root>");
            XElement newRoot = newDoc.Root;
            newRoot.Add(xusabugun);
            newDoc.Save("new filename");

【讨论】:

    猜你喜欢
    • 2016-09-05
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多