0

查询记录节点时如何获取 field.definition @name 属性?

这是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[jesse@turbokits.com]]></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[jesse@turbokits.com]]></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 = r.@id,
                .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)(r.@id, 0),
                    .Value = IsNull(Of String)(r.Value, Nothing),
                    .Type = IsNull(Of Long)(Convert.ToInt64(From n In _Fields.Ancestors("field.definition").Where(Function(i) i.@id = r.@id) Select n.@type), 0),
                    .Name = IsNull(Of String)((From n In _Fields.Ancestors("field.definition").Where(Function(i) i.@id = r.@id) Select n.@name).ToString(), Nothing)
                    }).ToList()
    End Function

我似乎遇到的问题是函数“无法将'WhereSelectEnumerableIterator`2 [System.Xml.Linq.XElement,System.String]'类型的对象转换为'System.IConvertible'。”

我要做的是填充记录的强类型列表,其中列表应包含记录 ID、字段值和字段名称

4

2 回答 2

0

我觉得

_rec = (From r In _x...<record>.AsParallel()
            Select New DatabaseTyping.Tables.Data() With {
                .Id = r.@id,
                .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) f.@id = r.@id)
                Select New DatabaseTyping.Tables.Field() With {
                    .Id = CType(r.@id, Long),
                    .Value = r.Value,
                    .Type = CType(fd.Attribute("type"), Long),
                    .Name = CType(fd.Attribute("name"), String)
                    }).ToList()
    End Function

应该做你想做的。

于 2012-06-23T09:36:03.560 回答
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) i.@id = r.@id).FirstOrDefault()
                    Select New DatabaseTyping.Tables.Field() With {
                        .Id = IsNull(Of Long)(r.@id, 0),
                        .Value = IsNull(Of String)(r.Value, Nothing),
                        .Type = IsNull(Of Long)(_fd.@type, 0),
                        .Name = IsNull(Of String)(_fd.@name, Nothing)
                    }).ToList()
    End Function
于 2012-06-25T12:33:24.770 回答