【问题标题】:Best Way to read specific attributes in XML [closed]在 XML 中读取特定属性的最佳方式 [关闭]
【发布时间】:2013-05-15 14:22:53
【问题描述】:

我正在尝试在我的自定义对象中读取此 xml 文件并需要一些帮助。最后,我想为每条记录创建员工列表。

<Records>
  <Record>
     <Item NAME="EMPLOYEE NAME">
        <IValue>Henry</IValue>
     </Item>
     <Item NAME="EMPLOYEE ID">
        <IValue>321</IValue>
     <Item>         
  </Record>
      ..More Record 
<Records>

员工类:

public class Employee
{
    public string EmployeeName {get;set;}
    public string EmployeeId {get;set;}
}

提前致谢。

【问题讨论】:

    标签: c# .net xml xml-parsing


    【解决方案1】:
    var employees = XDocument.Parse(xmlstring) //XDocument.Load(filename)
                        .Descendants("Record")
                        .Select(r => new Employee
                        {
                            EmployeeId = r.Elements("Item")
                                          .First(e => e.Attribute("NAME").Value == "EMPLOYEE ID").Value,
                            EmployeeName = r.Elements("Item")
                                           .First(e => e.Attribute("NAME").Value == "EMPLOYEE NAME").Value,
                        })
                        .ToList();
    

    【讨论】:

    • 错误:序列不包含数学元素。
    • @sanjeev40084 您的示例 xml 缺少关闭标签。我修复了它们并在上面的代码中使用。我工作完美。在不知道您的 real xml 的情况下,我无能为力了。只要知道,它已经过测试并且可以工作。
    • xml 包含除员工姓名、员工 ID(如员工部门、员工雇用日期等)之外的其他记录属性。
    • @sanjeev40084 你希望我猜你的xml并写代码吗?这就是我能给出的答案。至少表现出一些努力并尝试了解它的作用,它是如何做的......
    • 帮助 I4V。谢谢一堆
    【解决方案2】:

    怎么样

    // Open the xml file and create the element list
    XDocument doc = XDocument.Load("C://data.xml");
    var ElementList = doc.Root.Elements();
    
    // Crate the Employee list and populate
    List<Employee> list = new List<Employee>();
    foreach (XElement item in listaElementi)
    {
       list.Add(new Employee { EmployeeName = item.Descendants().First().Value, EmployeeId = item.Descendants().Last().Value });
    }
    

    Descendant().First() 和 Descendant().Last() 取决于 xml 的结构。通过这种方式,您将在内存中加载您的 xml。

    如果您要从 xml 中读取多个字段,我可以提出以下解决方案

      // You need a dictionary to map the columns of the data into the property of the
      //class since they have different names.
    
      Dictionary<string, string> dictionary = new Dictionary<string, string>();
      dictionary.Add("EMPLOYEE NAME", "EmployeeName");
      dictionary.Add("EMPLOYEE ID", "EmployeeId");
    
      XDocument doc = XDocument.Load("C://data.xml");
      var ElementList = doc.Root.Elements();
    
      List<Employee> list = new List<Employee>();
      foreach (XElement item in listaElementi)
          {
             // We read the properties from the xml
             var properties = item.Descendants().Where(p => p.HasAttributes);
                Employee employee = new Employee();
             // Using reflection and the dictionary we set the property value
             foreach (var property in properties)
                {
                    var t = property.FirstAttribute.Value;
                    var s = property.Value;
                    employee.GetType().GetProperty(dictionary[t]).SetValue(employee,s);
                }
             list.Add(employee);
    
    
            }
    

    【讨论】:

    • 如果 OP 将更多属性添加到记录中,则这是不可扩展的。
    • 这可能基于属性名称,我得到了更多属性,例如 (EmployeeDepartment, EmployeeHireDate..etc)??
    • 编辑了我的答案,希望对您有所帮助。
    猜你喜欢
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多