2012-06-22 120 views
0

當我查詢記錄節點時,如何獲取field.definition @name屬性?XDocument選擇祖先屬性

這裏是XML:

<?xml version="1.0" encoding="utf-8" ?> 
<Table> 
<fields> 
    <field.definition id="1" name="name" type="11" maxlength="250" default=""/> 
    <field.definition id="2" name="description" type="11" maxlength="250" default=""/> 
    <field.definition id="3" name="address" type="11" maxlength="250" default=""/> 
    <field.definition id="4" name="phone" type="11" maxlength="250" default=""/> 
    <field.definition id="5" name="email" type="11" maxlength="250" default=""/> 
</fields> 
<data> 
    <record id="1"> 
     <field id="1"><![CDATA[TurboKits, LLC]]></field> 
     <field id="2"><![CDATA[offering turbo kits and performance auto parts for all makes and models of vehicles. Our systems are manufactured to meet your high boost pressure and high horsepower needs.]]></field> 
     <field id="3"><![CDATA[1000 Old County Cir., #115 Windsor Locks, CT 06096]]></field> 
     <field id="4"><![CDATA[(860) 676-2929]]></field> 
     <field id="5"><![CDATA[[email protected]]]></field> 
    </record> 
    <record id="2"> 
     <field id="1"><![CDATA[Charles C Lewis]]></field> 
     <field id="2"><![CDATA[]]></field> 
     <field id="3"><![CDATA[209 Page Blvd., Springfield, MA 01101]]></field> 
     <field id="4"><![CDATA[413-733-2121]]></field> 
     <field id="5"><![CDATA[[email protected]]]></field> 
    </record> 
</data> 
</Table> 

這裏是我想要的代碼:我似乎有

'no filtering, no record selection going on, let's return all records 
    _rec = (From r In _x...<record>.AsParallel() 
      Select New DatabaseTyping.Tables.Data() With { 
       .Id = [email protected], 
       .Fields = ProcessFields(r) 
      }).ToList() 

    Private Function ProcessFields(ByVal _Fields As XElement) As IList(Of DatabaseTyping.Tables.Field) 
     Return (From r In _Fields...<field> 
       Select New DatabaseTyping.Tables.Field() With { 
        .Id = IsNull(Of Long)([email protected], 0), 
        .Value = IsNull(Of String)(r.Value, Nothing), 
        .Type = IsNull(Of Long)(Convert.ToInt64(From n In _Fields.Ancestors("field.definition").Where(Function(i) [email protected] = [email protected]) Select [email protected]), 0), 
        .Name = IsNull(Of String)((From n In _Fields.Ancestors("field.definition").Where(Function(i) [email protected] = [email protected]) Select [email protected]).ToString(), Nothing) 
        }).ToList() 
    End Function 

和問題中的函數「無法轉換的對象鍵入'WhereSelectEnumerableIterator`2 [System.Xml.Linq.XElement,System.String]'鍵入'System.IConvertible'。「

我所試圖做的是填充的記錄,在列表中應包含記錄ID,該字段值的強類型列表,並且字段名稱

回答

0

我認爲

_rec = (From r In _x...<record>.AsParallel() 
      Select New DatabaseTyping.Tables.Data() With { 
       .Id = [email protected], 
       .Fields = ProcessFields(r) 
      }).ToList() 

    Private Function ProcessFields(ByVal _Fields As XElement) As IList(Of DatabaseTyping.Tables.Field) 
     Return (From r In _Fields...<field> 
       Let fd = r.Document.<Table>.<fields>.<field.definition>.FirstOrDefault(Function(f) [email protected] = [email protected]) 
       Select New DatabaseTyping.Tables.Field() With { 
        .Id = CType([email protected], Long), 
        .Value = r.Value, 
        .Type = CType(fd.Attribute("type"), Long), 
        .Name = CType(fd.Attribute("name"), String) 
        }).ToList() 
    End Function 

應該做你想做的。

+0

實際上,我要關掉這個了不少。我做了一些記錄檢索的測試,發現一旦我開始觸及數千記錄標記,它就會顯着減慢。這是因爲我正在使用XDocument.Load。我將轉而成爲一個XmlReader – Kevin

0

還是有點簡單:

Private Function ProcessFields(ByVal _Fields As XElement, ByVal _FieldDefinitions As IEnumerable(Of XElement)) As IList(Of DatabaseTyping.Tables.Field) 
     Return (From r In _Fields...<field>.AsParallel() 
        Let _fd = _FieldDefinitions.<field.definition>.Where(Function(i) [email protected] = [email protected]).FirstOrDefault() 
        Select New DatabaseTyping.Tables.Field() With { 
         .Id = IsNull(Of Long)([email protected], 0), 
         .Value = IsNull(Of String)(r.Value, Nothing), 
         .Type = IsNull(Of Long)([email protected], 0), 
         .Name = IsNull(Of String)([email protected], Nothing) 
        }).ToList() 
    End Function