如何从XML的特定部分序列化XML到对象?



我有问题如何序列化XML到对象列表,但只能从一个大XML的特定部分。我的意思是这样的:

仅从项中获取数据,并生成项对象列表。

<tXML>
<nameOfUser>MK</nameOfUser>
<amouthOfPO>14</amouthOfPO>
<todayDate></todayDate>
<warehouses>
<warehouse id="1">
<name>AD1</name>
</warehouse>
<warehouse id="2">
<name>AD2</name>
</warehouse>
<warehouse id="3">
<name>AD3</name>
</warehouse>
</warehouses>
<items>
<warehause id="1">
<items>
<item>
<name>item1</name>
<protectionLevel>AMB</protectionLevel>
<description>DescAMB1FORID1</description>
</item>
<item>
<name>item2</name>
<protectionLevel>CHL</protectionLevel>
<description>DescCHL1FORID1</description>
</item>
<item>
<name>item3</name>
<protectionLevel>AMB</protectionLevel>
<description>3</description>
</item>
</items>
</warehause>
<warehause id="3">
<items>
<item>
<name>item1AMB2222</name>
<protectionLevel>AMBB222222</protectionLevel>
<description>DESCRIPTIONITEM1AM222222B</description>
</item>
<item>
<name>item222222CHL</name>
<protectionLevel>C222222222LL</protectionLevel>
<description>ITEM2CH22ILLERAD1</description>
</item>
<item>
<name>2222222222223</name>
<protectionLevel>222222223</protectionLevel>
<description>3222222222222</description>
</item>
</items>
</warehause>
<warehause id="3">
<items>
<item>
<name>item1333333AMB</name>
<protectionLevel>AM3333BB</protectionLevel>
<description>DESCR333IPTIONITEM1AMB</description>
</item>
<item>
<name>item233333CHL</name>
<protectionLevel>C33333HLL</protectionLevel>
<description>ITEM2CHI333LLERAD1</description>
</item>
<item>
<name>33</name>
<protectionLevel>33</protectionLevel>
<description>33</description>
</item>
</items>
</warehause>
</items>
</tXML>

我的item类如下所示:

namespace UseFiles
{
public class Item
{
public string Name { get; set; }
public string ProtectionLevel { get; set; }
public string Description { get; set; }
}
}

为对象更改XML(仅从)的最佳(最优)方法是什么?我想到了两种方法:

  1. xmlSerializer
  2. 或者只是在XMLNodeList的循环中赋值。

首先,best (optimally) way可以解释为:1 -最佳性能,2 -更(容易)可读的代码。

我认为最好的性能应该是使用XmlReaderXmlWriterFileStream一起工作。但在您的情况下,这可能会导致一些额外的复杂性,因为items位于wherehouses内,而任何item都应该位于特定的"wherehouse"内。因此,您应该找到包含具体wherehouse的项目,并在一些更新后将这些项目保存在相同的wherehouse中。从wherehouse中,任何项都可以:1 -更新,2 -添加,3 -删除,因此更新现有的xml文档变得更加复杂。

基于以上,我推荐使用LinqToXml,它不是那么高效,但在大多数情况下足够快,有你需要的任何工具(在具体的xml标签中搜索和替换元素列表),并且更具可读性。

你可以这样使用它:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace TestProgram
{
class Program
{
private const string _PATH = @"path to xml-file";
static void Main(string[] args)
{
const string myWherehouseId = "3";

var document = XDocument.Load(_PATH);
var itemsElement = document.Root.Element("items");
List<Item> myItems = null;
foreach (XElement wherehause in itemsElement.Elements())
{
if (wherehause.Attribute("id").Value == myWherehouseId)
{
myItems = FromXml(wherehause.Element("items").Elements());
// Do some things
myItems[0].Name += "1";
myItems[0].Description += "2";
myItems.RemoveAt(1);
myItems.Add(
new Item
{ 
Name = "Name212",
Description = "Sometext123213",
ProtectionLevel = "CHL"
});
XElement[] updatedElements = ToXml(myItems);
// replace old xml nodes with new ones
wherehause.Element("items").ReplaceNodes(updatedElements);
break;
}
}
document.Save(_PATH);
}
public static List<Item> FromXml(IEnumerable<XElement> elements)
{
// you could use any custom mapper for conversion from XElement to Item
return elements.Select(el => new Item
{
Name = el.Element("name").Value,
ProtectionLevel = el.Element("protectionLevel").Value,
Description = el.Element("description").Value                
}).ToList();
}
public static XElement[] ToXml(List<Item> items)
{
// you could use any custom mapper for conversion from Item to XElement
return items
.Select(item => 
new XElement("item", 
new XElement("name", item.Name),
new XElement("protectionLevel", item.ProtectionLevel),
new XElement("description", item.Description)))
.ToArray();
}
}
public class Item
{
public string Name { get; set; }
public string ProtectionLevel { get; set; }
public string Description { get; set; }
}
}

在我看来,best (optimally) way包含了性能和代码可读性之间的合理折衷。

这是我最好的方法;)

XmlReader是解析大型xml文件的好方法。或者你可以尝试Cinchoo ETL——一个开源库,用几行代码解析如此大的文件。

using (var r = ChoXmlReader<Item>.LoadText(xml)
.WithXPath("//item")
)
{
foreach (var rec in r)
rec.Print();
}

示例小提琴:https://dotnetfiddle.net/otYq5j

声明:我是这个库的作者。

最新更新