从 List更新 XML 文件
我有一个包含以下记录的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作:
然后您可以将修改后的 xDocument 保存回文件或其他文件中。
如果您发现这不够高效,有多种方法可以加快速度。例如,通过
CId
创建元素的字典
,这样就不会每次都搜索整个文档。但是,如果您有巨大的文件,将它们整个加载到内存中可能是不可能的,也不是一个好主意。使用
XmlReader
和XmlWriter
适用于任何大小的文件,但它们不太容易使用。另一个需要考虑的选项是 XML 序列化。它专门用于将 XML 与对象相互转换。
此外,您的代码可以大大简化,并且在此过程中变得更快:
You could do something like this:
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 byCId
, 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
andXmlWriter
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: