如何将 XML 序列化为 XML 特定部分的对象?
How to serialize XML to object from particular part of 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>
我的商品 class 如下所示:
namespace UseFiles
{
public class Item
{
public string Name { get; set; }
public string ProtectionLevel { get; set; }
public string Description { get; set; }
}
}
更改对象的 XML(仅从 )的最佳(最优)方法是什么?我想了两个办法:
- xmlSerializer
- 或者只是在 XMLNodeList ..
循环中赋值
首先 best (optimally) way
可以解释为:1 - 最佳性能,2 - 更(简单)可读的代码。
我认为最好的表现应该是 FileStream
使用 XmlReader
和 XmlWriter
。但在你的情况下,这可能会导致一些额外的并发症,因为 items
位于 wherehouses
内,任何 item
都应该位于特定的 'wherehouse' 内。所以你应该找到包含具体 wherehouse
的项目,并在一些更新后将这些项目保存在相同的 wherehouse
中。任何项目都可以:1 - 更新,2 - 添加,3 - 删除,来自 wherehouse
,因此更新现有 xml-文档变得更加复杂。
基于以上,我建议使用 LinqToXml
,它的性能效率不高,但足够快在大多数情况下,有任何你需要的工具(搜索和替换具体 xml-tag 中的元素列表)并且更具可读性。
你可以这样使用它:
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();
}
样本fiddle:https://dotnetfiddle.net/otYq5j
免责声明:我是该库的作者。
我在如何将 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>
我的商品 class 如下所示:
namespace UseFiles
{
public class Item
{
public string Name { get; set; }
public string ProtectionLevel { get; set; }
public string Description { get; set; }
}
}
更改对象的 XML(仅从 )的最佳(最优)方法是什么?我想了两个办法:
- xmlSerializer
- 或者只是在 XMLNodeList .. 循环中赋值
首先 best (optimally) way
可以解释为:1 - 最佳性能,2 - 更(简单)可读的代码。
我认为最好的表现应该是 FileStream
使用 XmlReader
和 XmlWriter
。但在你的情况下,这可能会导致一些额外的并发症,因为 items
位于 wherehouses
内,任何 item
都应该位于特定的 'wherehouse' 内。所以你应该找到包含具体 wherehouse
的项目,并在一些更新后将这些项目保存在相同的 wherehouse
中。任何项目都可以:1 - 更新,2 - 添加,3 - 删除,来自 wherehouse
,因此更新现有 xml-文档变得更加复杂。
基于以上,我建议使用 LinqToXml
,它的性能效率不高,但足够快在大多数情况下,有任何你需要的工具(搜索和替换具体 xml-tag 中的元素列表)并且更具可读性。
你可以这样使用它:
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();
}
样本fiddle:https://dotnetfiddle.net/otYq5j
免责声明:我是该库的作者。