我有問題如何將 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>
我的專案類如下所示:
namespace UseFiles
{
public class Item
{
public string Name { get; set; }
public string ProtectionLevel { get; set; }
public string Description { get; set; }
}
}
為物件更改 XML(僅從 )的最佳(最佳)方法是什么?我想到了兩種方法:
- xml序列化器
- 或者只是從 XMLNodeList 回圈分配值..
uj5u.com熱心網友回復:
首先best (optimally) way可以解釋為:1 - 最佳性能,2 - 更多(容易)可讀的代碼。
我認為最好的性能應該是FileStream使用XmlReader和XmlWriter。但是在您的情況下,這可能會導致一些額外的并發癥,因為items位于 inside 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包括性能和代碼可讀性之間的合理折衷。
這是我最好的方法;)
uj5u.com熱心網友回復:
好吧,XmlReader 用于決議大型 xml 檔案的方法。或者,您可以嘗試Cinchoo ETL - 一個開源庫,可以用幾行代碼決議這么大的檔案。
using (var r = ChoXmlReader<Item>.LoadText(xml)
.WithXPath("//item")
)
{
foreach (var rec in r)
rec.Print();
}
示例小提琴:https : //dotnetfiddle.net/otYq5j
免責宣告:我是這個庫的作者。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/337086.html
