Анализ XML файла в списке объектов

Как правильно загрузить следующий XML? Мне нужно получить все значения под ClassDetails в каждом дочернем узле.

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

Мой POCO

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.******</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.******</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.******</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.******</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>******* Ticket</parent>
 <type>System.Int32</type>
 <length>256</length>
 <key>false</key>
 <required>false</required>
 </classdetails>
 <classdetails>
 <name>Impact</name>
 <displayname>Impact</displayname>
 <parent>******* 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>******* 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>******* 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>******* 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;
3 ответа

Descendents - хороший вариант, если вы не хотите фильтровать, просто чтобы получить "все". Документацию по XDocument.Descendents можно найти в 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 как указано @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;


У ваших элементов есть пространство имен, поэтому вы должны указать пространство имен при их получении:

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

licensed under cc by-sa 3.0 with attribution.