I have following XML File:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<T3RecordDocument>
<header type="array">
<charset>utf-8</charset>
<XMLversion>1.0</XMLversion>
<meta type="array">
<title></title>
<description></description>
<notes></notes>
<packager_username>xxxadmin</packager_username>
<packager_name>Firstname</packager_name>
<packager_email>name@domain.com</packager_email>
<TYPO3_version>4.5.26</TYPO3_version>
<created>Thursday 27. June 2013</created>
</meta>
<static_tables index="relStaticTables" type="array">
</static_tables>
<excludeMap type="array">
</excludeMap>
<softrefCfg type="array">
</softrefCfg>
<extensionDependencies type="array">
</extensionDependencies>
<records type="array">
<table index="tx_nfcacedata_lawyer" type="array">
<rec index="678" type="array">
<uid>678</uid>
<pid>233</pid>
<title>Partner & Partner, City</title>
<size>536</size>
<relations index="rels" type="array">
<element index="tx_nfcacedata_county:137" type="array">
<id>137</id>
<table>tx_nfcacedata_county</table>
</element>
</relations>
<softrefs type="array">
</softrefs>
</rec>
<rec index="679" type="array">
<uid>679</uid>
<pid>233</pid>
<title>Name2, City2</title>
<size>530</size>
<relations index="rels" type="array">
<element index="tx_nfcacedata_county:137" type="array">
<id>137</id>
<table>tx_nfcacedata_county</table>
</element>
</relations>
<softrefs type="array">
</softrefs>
</rec>
</table>
</records>
<pid_lookup type="array">
<page_contents index="233" type="array">
<table index="tx_nfcacedata_lawyer" type="array">
<item index="678">1</item>
<item index="679">1</item>
</table>
</page_contents>
</pid_lookup>
</header>
<records type="array">
<tablerow index="tx_nfcacedata_lawyer:678" type="array">
<fieldlist index="data" type="array">
<field index="uid">678</field>
<field index="pid">233</field>
<field index="tstamp">1321450985</field>
<field index="crdate">1250858888</field>
<field index="cruser_id">2</field>
<field index="deleted">0</field>
<field index="name">Partner & Partner</field>
<field index="street">Street 49</field>
<field index="zip">137</field>
<field index="phone">0123 123456</field>
<field index="fax">0123 1234560</field>
<field index="web">www.domain.de</field>
<field index="mail">info@domain.de</field>
<field index="mobile"></field>
<field index="zip_internal">12345</field>
<field index="city_internal">City</field>
<field index="latitude">10.0347062</field>
<field index="longitude">20.7524338</field>
</fieldlist>
<related index="rels" type="array">
<field index="zip" type="array">
<type>db</type>
<relations index="itemArray" type="array">
<element index="0" type="array">
<id>137</id>
<table>tx_nfcacedata_county</table>
</element>
</relations>
</field>
</related>
</tablerow>
<tablerow index="tx_nfcacedata_lawyer:679" type="array">
<fieldlist index="data" type="array">
<field index="uid">679</field>
<field index="pid">233</field>
<field index="tstamp">1257856437</field>
<field index="crdate">1250858888</field>
<field index="cruser_id">2</field>
<field index="deleted">0</field>
<field index="name">Name2</field>
<field index="street">Street 5</field>
<field index="zip">137</field>
<field index="phone">0234 12345678</field>
<field index="fax">0234 123456780</field>
<field index="web">www.domain2.de</field>
<field index="mail">info@domain2.de</field>
<field index="mobile"></field>
<field index="zip_internal">12345</field>
<field index="city_internal">City2</field>
<field index="latitude">10.0523024</field>
<field index="longitude">20.8061242</field>
</fieldlist>
<related index="rels" type="array">
<field index="zip" type="array">
<type>db</type>
<relations index="itemArray" type="array">
<element index="0" type="array">
<id>137</id>
<table>tx_nfcacedata_county</table>
</element>
</relations>
</field>
</related>
</tablerow>
</records>
</T3RecordDocument>
And I am currently parsing it with following code:
XDocument xdoc = XDocument.Parse(XML);
foreach (var descendant in xdoc.Descendants("records").Descendants("tablerow").Descendants("fieldlist"))
{
Lawyer tempLawyer = new Lawyer();
foreach (var item in descendant.Elements())
{
switch (item.Attributes().First().Value)
{
case "name":
tempLawyer.name = item.Value;
break;
case "street":
tempLawyer.street = item.Value;
break;
case "zip_internal":
tempLawyer.zip = item.Value;
break;
case "city_internal":
tempLawyer.city = item.Value;
break;
case "phone":
tempLawyer.phone = item.Value;
break;
case "fax":
tempLawyer.fax = item.Value;
break;
case "web":
tempLawyer.web = item.Value;
break;
case "latitude":
tempLawyer.lat = item.Value;
break;
case "longitude":
tempLawyer.lng = item.Value;
break;
}
}
list_Lawyer.Add(tempLawyer);
}
It's working, but maybe there is a more simpler and faster way to achieve what I want.
Maybe something like this:
var tempList = from element in
xdoc.Descendants("records").Descendants("tablerow").Descendants("fieldlist").Elements()
select new
{
name = (string)element.Attribute("name").Value,
street = (string)element.Attribute("street").Value,
zip = (string)element.Attribute("zip_internal").Value,
city = (string)element.Attribute("city_internal").Value,
phone = (string)element.Attribute("phone").Value,
fax = (string)element.Attribute("fax").Value,
web = (string)element.Attribute("web").Value,
lat = (string)element.Attribute("latitude").Value,
lng = (string)element.Attribute("longitude").Value,
};
This code isn't working because the element's attribute is index="XXXXXX" and not XXXXXX, maybe someone can lead me into the right direction.