所以基本上,我正在读入两个XML文档。第一个有两个需要存储的值: Name和Value。第二个值有四个值: Name、DefaultValue、Type和Limit。在读取文档时,我希望将每个文档存储到某个对象中。我需要能够将这两个对象组合成一个有5个值的对象。XML文档的长度不同,但第二个文档的大小始终与第一个文档的大小相同。
示例:
<XML1>
<Item1>
<Name>Cust_No</Name>
<Value>10001</Value>
</Item1>
<Item4>
ITEM4 NAME AND VALUE
</Item4>
<Item7>
ITEM 7 NAME AND VALUE
</Item7>
</XML1>
<XML2>
<Item1>
<Name>Cust_No</Name>
<DefaultValue></DefaultValue>
<Type>varchar</Type>
<Limit>15</Limit>
</Item1>
6 MORE TIMES ITEMS 2-7
</XML2>我已经有了遍历XML的代码。我真的需要一些想法来存储数据的最好的方式。最终,我希望能够在Name键上连接这两个对象。我尝试了string[]和arrayList[],但我遇到了将它们组合在一起的困难。我也阅读了Dictionary,但在实现时也遇到了问题(我以前从未使用过Dictionary )。
发布于 2013-02-21 01:52:50
这是Linq to Xml查询,它将连接两个XDocuments并为连接的项选择匿名对象。每个对象都有五个属性:
var query =
from i1 in xdoc1.Root.Elements()
join i2 in xdoc2.Root.Elements()
on (string)i1.Element("Name") equals (string)i2.Element("Name") into g
let j = g.SingleOrDefault() // get joined element from second file, if any
select new {
Name = g.Key,
Value = (int)i1.Element("Value"),
DefaultValue = (j == null) ? null : (string)j.Element("DefaultValue"),
Type = (j == null) ? null : (string)j.Element("Type"),
Limit = (j == null) ? null : (string)j.Element("Limit")
};XDocuments是这样创建的:
var xdoc1 = XDocument.Load(path_to_xml1);
var xdoc2 = XDocument.Load(path_to_xml2);查询的用法:
foreach(var item in query)
{
// use string item.Name
// integer item.Value
// string item.DefaultValue
// string item.Type
// string item.Limit
}https://stackoverflow.com/questions/14986444
复制相似问题