XDocument选择祖先属性

时间:2012-06-22 20:24:53

标签: .net vb.net visual-studio-2010 linq-to-xml

当我查询记录节点时,如何获取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'。”< / p>

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

2 个答案:

答案 0 :(得分: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

应该做你想做的事。

答案 1 :(得分: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