从 List更新 XML 文件

发布于 2024-12-19 14:29:30 字数 1336 浏览 0 评论 0原文

我有一个包含以下记录的 XML 文件 -

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCLocation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CLocation>
    <CId>5726</CId>
    <Long>0</Long>
    <Lat>0</Lat>
    <Status>Pending</Status>
  </CLocation>
  <CLocation>
    <CId>5736</CId>
    <Long>0</Long>
    <Lat>0</Lat>
    <Status>Processed</Status>        
  </CLocation>
</ArrayOfCLocation>

我将这些记录放入列表中 -

XDocument xDocument = XDocument.Load(filePath); 
List<T> list = xDocument.Descendants("CLocation")
     .Select(c => (new T()
     {
         CId = Convert.ToInt32(c.Descendants("CId").FirstOrDefault().Value),
         Lat = Convert.ToDouble(c.Descendants("Lat").FirstOrDefault().Value),
         Long = Convert.ToDouble(c.Descendants("Long").FirstOrDefault().Value),
         Status = (Status)Enum.Parse(typeof(Status), c.Descendants("Status").FirstOrDefault().Value)
     }))
     .Where(c => c.Status == Status.Pending)
     .Take(listCount)
     .ToList();

现在,我更新上面集合中的 T 对象(设置其纬度/日志字段) 处理这些对象后,我想将这些记录更新回 XML 文件。

任何人都可以指导我如何将这些对象更新回 XML 文件的有效解决方案。

I have a XML file containing records like -

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCLocation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CLocation>
    <CId>5726</CId>
    <Long>0</Long>
    <Lat>0</Lat>
    <Status>Pending</Status>
  </CLocation>
  <CLocation>
    <CId>5736</CId>
    <Long>0</Long>
    <Lat>0</Lat>
    <Status>Processed</Status>        
  </CLocation>
</ArrayOfCLocation>

I take these records into List as -

XDocument xDocument = XDocument.Load(filePath); 
List<T> list = xDocument.Descendants("CLocation")
     .Select(c => (new T()
     {
         CId = Convert.ToInt32(c.Descendants("CId").FirstOrDefault().Value),
         Lat = Convert.ToDouble(c.Descendants("Lat").FirstOrDefault().Value),
         Long = Convert.ToDouble(c.Descendants("Long").FirstOrDefault().Value),
         Status = (Status)Enum.Parse(typeof(Status), c.Descendants("Status").FirstOrDefault().Value)
     }))
     .Where(c => c.Status == Status.Pending)
     .Take(listCount)
     .ToList();

Now, I update T objects(setting their Lat/Log fields) in above collection
and after processing these objects, I want to update these records back into XML file.

Can anyone please guide me for a efficient solution for how can I update these objects back into XML file.

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

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

发布评论

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

评论(1

最笨的告白 2024-12-26 14:29:30

您可以执行以下操作:

foreach (var location in list)
{
    var elem = xDocument.Root.Elements()
                        .Single(e => (int)e.Element("CId") == location.CId);
    elem.Element("Long").ReplaceNodes(location.Long);
    elem.Element("Lat").ReplaceNodes(location.Lat);
}

然后您可以将修改后的 xDocument 保存回文件或其他文件中。

如果您发现这不够高效,有多种方法可以加快速度。例如,通过CId创建元素的字典,这样就不会每次都搜索整个文档。

但是,如果您有巨大的文件,将它们整个加载到内存中可能是不可能的,也不是一个好主意。使用 XmlReaderXmlWriter 适用于任何大小的文件,但它们不太容易使用。

另一个需要考虑的选项是 XML 序列化。它专门用于将 XML 与对象相互转换。

此外,您的代码可以大大简化,并且在此过程中变得更快:

xDocument.Root.Elements("CLocation")
     .Select(c => new Location
                  {
                      CId = (int)c.Element("CId"),
                      Lat = (double)c.Element("Lat"),
                      Long = (double)c.Element("Long"),
                      Status = (Status)Enum.Parse(typeof(Status), c.Element("Status").Value)
                  })

You could do something like this:

foreach (var location in list)
{
    var elem = xDocument.Root.Elements()
                        .Single(e => (int)e.Element("CId") == location.CId);
    elem.Element("Long").ReplaceNodes(location.Long);
    elem.Element("Lat").ReplaceNodes(location.Lat);
}

You can then save the modified xDocument back to a file, or whatever.

If you find this is not efficient enough, there are several ways to speed things up. For example create a Dictionary of elements by CId, so that the whole document is not searched every time.

But if you have huge files, loading them whole into memory might not be possible or a good idea. Using XmlReader and XmlWriter will work for files of any size, but they are not as easy to use.

Another option to consider is XML serialization. It's made specifically for converting XML into your objects and back.

Also, the code you have could be simplified quite a lot, and in the process made faster:

xDocument.Root.Elements("CLocation")
     .Select(c => new Location
                  {
                      CId = (int)c.Element("CId"),
                      Lat = (double)c.Element("Lat"),
                      Long = (double)c.Element("Long"),
                      Status = (Status)Enum.Parse(typeof(Status), c.Element("Status").Value)
                  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文