将XML文件解析为对象列表

本文关键字:对象 列表 XML 文件 | 更新日期: 2023-09-27 18:12:34

如何正确加载以下XML ?我需要检索每个子节点中ClassDetails下的所有值。

public void GetXmlFormValues()
{
   XDocument xdoc = XDocument.Load(@"c:'form7.txt");
   var query = from datas in xdoc.Root.Elements("ArrayOfArrayOfClassDetails")
               select datas;
  }

My POCO is

public class FormClass
{
    public string name { get; set; }
    public string displayName { get; set; }
    public string length { get; set; }
    public Boolean key { get; set; }
    public Boolean required { get; set; }         
}  

XML输入为

  <?xml version="1.0" encoding="UTF-8" ?>
  <ArrayOfArrayOfClassDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
    <ArrayOfClassDetails>
      <ClassDetails>
        <name>DisplayName</name>
        <displayName>Display Name</displayName>
        <parent>Object</parent>
        <type>System.String</type>
        <length>4000</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
    </ArrayOfClassDetails>
    <ArrayOfClassDetails>
      <ClassDetails>
        <name>Id</name>
        <displayName>ID</displayName>
        <parent>Work Item</parent>
        <type>System.String</type>
        <length>256</length>
        <key>true</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>Title</name>
        <displayName>Title</displayName>
        <parent>Work Item</parent>
        <type>System.String</type>
        <length>200</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>Description</name>
        <displayName>Description</displayName>
        <parent>Work Item</parent>
        <type>System.String</type>
        <length>4000</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ContactMethod</name>
        <displayName>Alternate contact method</displayName>
        <parent>Work Item</parent>
        <type>System.String</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>CreatedDate</name>
        <displayName>Created date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ScheduledStartDate</name>
        <displayName>Scheduled start date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ScheduledEndDate</name>
        <displayName>Scheduled end date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ActualStartDate</name>
        <displayName>Actual start date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ActualEndDate</name>
        <displayName>Actual end date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>IsDowntime</name>
        <displayName>Is Downtime</displayName>
        <parent>Work Item</parent>
        <type>System.Boolean</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>IsParent</name>
        <displayName>Is parent</displayName>
        <parent>Work Item</parent>
        <type>System.Boolean</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ScheduledDowntimeStartDate</name>
        <displayName>Scheduled Downtime Start Date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ScheduledDowntimeEndDate</name>
        <displayName>Scheduled Downtime End Date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ActualDowntimeStartDate</name>
        <displayName>Actual Downtime Start Date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ActualDowntimeEndDate</name>
        <displayName>Actual Downtime End Date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>RequiredBy</name>
        <displayName>Required By</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>PlannedCost</name>
        <displayName>Planned Cost</displayName>
        <parent>Work Item</parent>
        <type>System.Double</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ActualCost</name>
        <displayName>Actual Cost</displayName>
        <parent>Work Item</parent>
        <type>System.Double</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>PlannedWork</name>
        <displayName>Planned Work Hours</displayName>
        <parent>Work Item</parent>
        <type>System.Double</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ActualWork</name>
        <displayName>Actual Work Hours</displayName>
        <parent>Work Item</parent>
        <type>System.Double</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>UserInput</name>
        <displayName>User Input</displayName>
        <parent>Work Item</parent>
        <type>System.String</type>
        <length>65536</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>FirstAssignedDate</name>
        <displayName>First assigned date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>FirstResponseDate</name>
        <displayName>First response date</displayName>
        <parent>Work Item</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
    </ArrayOfClassDetails>
    <ArrayOfClassDetails>
      <ClassDetails>
        <name>Priority</name>
        <displayName>Priority</displayName>
        <parent>Trouble Ticket</parent>
        <type>System.Int32</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>Impact</name>
        <displayName>Impact</displayName>
        <parent>Trouble Ticket</parent>
        <type>System.Enum</type>
        <length>256</length>
        <key>false</key>
        <required>true</required>
        <enumType>11756265-f18e-e090-eed2-3aa923a4c872</enumType>
      </ClassDetails>
      <ClassDetails>
        <name>Urgency</name>
        <displayName>Urgency</displayName>
        <parent>Trouble Ticket</parent>
        <type>System.Enum</type>
        <length>256</length>
        <key>false</key>
        <required>true</required>
        <enumType>04b28bfb-8898-9af3-009b-979e58837852</enumType>
      </ClassDetails>
      <ClassDetails>
        <name>ClosedDate</name>
        <displayName>Closed date</displayName>
        <parent>Trouble Ticket</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>ResolvedDate</name>
        <displayName>Resolved date</displayName>
        <parent>Trouble Ticket</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
    </ArrayOfClassDetails>
    <ArrayOfClassDetails>
      <ClassDetails>
        <name>TargetResolutionTime</name>
        <displayName>Resolve by</displayName>
        <parent>Incident</parent>
        <type>System.DateTime</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>Escalated</name>
        <displayName>Escalated</displayName>
        <parent>Incident</parent>
        <type>System.Boolean</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>Source</name>
        <displayName>Source</displayName>
        <parent>Incident</parent>
        <type>System.Enum</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
        <enumType>5d59071e-69b3-7ef4-6dee-aacc5b36d898</enumType>
      </ClassDetails>
      <ClassDetails>
        <name>Status</name>
        <displayName>Status</displayName>
        <parent>Incident</parent>
        <type>System.Enum</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
        <enumType>89b34802-671e-e422-5e38-7dae9a413ef8</enumType>
      </ClassDetails>
      <ClassDetails>
        <name>ResolutionDescription</name>
        <displayName>Resolution description</displayName>
        <parent>Incident</parent>
        <type>System.String</type>
        <length>4000</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>NeedsKnowledgeArticle</name>
        <displayName>Needs knowledge article</displayName>
        <parent>Incident</parent>
        <type>System.Boolean</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>TierQueue</name>
        <displayName>Support group</displayName>
        <parent>Incident</parent>
        <type>System.Enum</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
        <enumType>c3264527-a501-029f-6872-31300080b3bf</enumType>
      </ClassDetails>
      <ClassDetails>
        <name>HasCreatedKnowledgeArticle</name>
        <displayName>Has created knowledge article</displayName>
        <parent>Incident</parent>
        <type>System.Boolean</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>
      <ClassDetails>
        <name>LastModifiedSource</name>
        <displayName>Last modified source</displayName>
        <parent>Incident</parent>
        <type>System.Enum</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
        <enumType>5d59071e-69b3-7ef4-6dee-aacc5b36d898</enumType>
      </ClassDetails>
      <ClassDetails>
        <name>Classification</name>
        <displayName>Classification category</displayName>
        <parent>Incident</parent>
        <type>System.Enum</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
        <enumType>1f77f0ce-9e43-340f-1fd5-b11cc36c9cba</enumType>
      </ClassDetails>
      <ClassDetails>
        <name>ResolutionCategory</name>
        <displayName>Resolution category</displayName>
        <parent>Incident</parent>
        <type>System.Enum</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
        <enumType>72674491-02cb-1d90-a48f-1b269eb83602</enumType>
      </ClassDetails>
    </ArrayOfClassDetails>
  </ArrayOfArrayOfClassDetails>

编辑仍然没有结果,很奇怪

 XElement xdoc = XElement.Load(@"c:'form.xml");
        var classDetails = from classDetail in xdoc.Descendants("ClassDetails")
                           select new FormClass
                           {
                               name = (string)classDetail.Element("name"),
                               displayName = (string)classDetail.Element("displayName"),
                               length = (int)classDetail.Element("length"),
                               key = (bool)classDetail.Element("key"),
                               required = (bool)classDetail.Element("required"),
                           };
        return classDetails;

将XML文件解析为对象列表

Descendents是一个很好的选择,如果你不想过滤,只是为了得到"一切"。XDocument上的文档。后代可以在MSDN上找到。

确保在你的类文件中包含对System.Xml.Linq的引用。

下面是一个将XML解析为所需类型的选项:

 XDocument xdoc = XDocument.Load(@"c:'form7.txt");
 var classDetails = from classDetail in xdoc.Descendents("ClassDetails")
               select new FormClass
               {
                  name = (string)classDetail.Element("name"),
                  displayName = (string)classDetail.Element("displayName"),
                  length = (int)classDetail.Element("length"),
                  key = (bool)classDetail.Element("key"),
                  required = (bool)classDetail.Element("required"),
               };

你的元素有一个命名空间所以你必须在获取它们时指定命名空间:

XNamespace ns = xdoc.GetDefaultNamespace();
var query = from datas in xdoc.Root.Elements(ns + "ArrayOfArrayOfClassDetails")
                select datas;

回应您的编辑部分…您需要使用XNamespace作为@Selman22所指向的,因为XML有默认的命名空间声明:

XElement xdoc = XElement.Load(@"c:'form.xml");
XNamespace ns = xdoc.GetDefaultNamespace();
var classDetails = from classDetail in xdoc.Descendants(ns + "ClassDetails")
                   select new FormClass
                   {
                       name = (string)classDetail.Element(ns + "name"),
                       displayName = (string)classDetail.Element(ns + "displayName"),
                       length = (int)classDetail.Element(ns + "length"),
                       key = (bool)classDetail.Element(ns + "key"),
                       required = (bool)classDetail.Element(ns + "required"),
                   };
return classDetails;