0

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 &amp; 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 &amp; 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.


Join tables (1:n) with considering two records from the second table

I have two tables named letter and letter_person like this :

letter :

id | subject | body | time
==========================
 1 |  Hello  | null | ...
 .
 .
 .

and letter_person :

id | ref_person | ref_letter |   role
========================================
 1 |     1      |     1      |   sender
 2 |     2      |     1      |  receiver
 .
 .
 .

role is an enum field and can be sender or receiver

i want to select letters which are sent and received between two persons, i want something like this :

SELECT * FROM letter
JOIN letter_person ON letter_person.ref_letter = letter
WHERE
(letter_person.ref_person = 1 AND letter_person.role = 'sender') 
OR
(letter_person.ref_person = 2 AND letter_person.role = 'receiver')

But it's not exactly what i need.

This query selects all the letters which are sent by person1 + all the letters which are received by person2.

I need to get the conversation.

4

1 回答 1

1
from element in xdoc.Descendants("fieldlist")
select new { 
    Name = element.Elements("field")
                  .Single(e => e.Attribute("index").Value == "name")
                  .Value,
    ...
于 2013-11-14T20:32:52.957 回答