【问题标题】:How to add XML attribute value to a dictionary containg an object如何将 XML 属性值添加到包含对象的字典中
【发布时间】: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


    【解决方案1】:

    尝试以下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
    
                XDocument doc = XDocument.Load(FILENAME);
    
                List<Component> components = doc.Descendants("Part").Select(x => new Component()
                {
                    ComponentNr = (string)x.Attribute("No"),
                    Omschrijving = (string)x.Elements().Where(y => y.Attribute("PartsName") != null).Select(y => (string)y.Attribute("PartsName")).FirstOrDefault(),
                    Pos = (int)x.Descendants().Where(y => y.Attribute("SetNo") != null).Select(y => (int)y.Attribute("SetNo")).FirstOrDefault()
                }).ToList();
    
                Dictionary<string, Component> dict = components
                    .GroupBy(x => x.ComponentNr, y => y)
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());
    
            }
        }
       public class Component
        {
            public string ComponentNr { get; set; }
            public string Omschrijving { get; set; }
            public int Aantal { get; set; }
            public int Pos { get; set; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多