【发布时间】:2023-04-02 20:00:01
【问题描述】:
我有一个字典,其中包含一个作为 TKey 的字符串和一个作为 TValue 的类“组件”。 键是我从数据库中获取的值,而值来自 XML 文件。 在我的第一种方法中,我用数据库中的键填充我的字典,在第二种方法中,我想获取键与 xml 文件中的字符串相同的位置。
这是我的 xml 文件的一个小例子:
<Part No="1">
<Part_001 PartsName="38392000" /><br>
<part_003 SetNo="12" />
</Part>
<Part No="2">
<Part_001 PartsName="37625800" /><br>
<part_003 SetNo="13" />
</Part>
...
等等……
PartsName与key值相同,SetNo为Pos
班级:
public class Component
{
public string ComponentNr { get; set; }
public string Omschrijving { get; set; }
public int Aantal { get; set; }
public int Pos { get; set; }
}
这是我从数据库中检索键值的地方:
Dictionary<string, Component> resultaten = new Dictionary<string, Component>();
Component component = new Component();
if (resultaten.ContainsKey((string)dgReader["artcode"]))
{
resultaten.Add
(
(string)dgReader["artcode"],
component
);
}
这是我想要获取 xml 值 SetNo 并将其添加到类字段 Pos 的地方:
Dictionary<string, Component> resultaten = new Dictionary<string, Component>();
var query = (from p in xdoc.Descendants("Part")
where (int)p.Attribute("No") > 0
select new
{
ComponentNr = p.Element("Part_001").Attribute("PartsName").Value,
Pos = Convert.ToInt32(p.Element("Part_003").Attribute("Setno").Value)
});
foreach (var item in query)
{
//code to add to dictionary resultaten
}
键和值的检索通过两种不同的方法进行
【问题讨论】:
标签: c# sql-server xml linq