0

私はこのxmlファイルを持っています

<config>
  <PersonMapping>
    <Field>
      <Name>Id</Name>
      <Position>0</Position>
    </Field>
    <Field>
      <Name>FirstName</Name>
      <Position>1</Position>
    </Field>
    <Field>
      <Name>LastName</Name>
      <Position>2</Position>
    </Field>
    <Field>
      <Name insert='false' update='false'>Address1</Name>
      <Position>3</Position>
    </Field>
    <Field>
      <Name insert='false' update='false'>Address2</Name>
      <Position>4</Position>
    </Field>
  </PersonMapping>
</config>

このファイルの設定に基づいて、2つのコレクションを作成する必要があります。ユーザーのニーズに応じて、特定の「フィールド」タグに「挿入」属性と「更新」属性がある場合とない場合があります。

挿入コレクションには、挿入='true'または存在しないすべてのタグが含まれます更新コレクションには、更新='true'または存在しないすべてのタグが含まれます

どちらも持たないタグの場合、デフォルトでtrueになります。

私は挿入のためにこのクエリを書きました

propertiesToInsertFromXML = from nameTag in xml.Element("Config").Element("PersonMapping").Elements("Field")
                            let insert = nameTag.Element("Name").Attribute("insert")
                            let update = nameTag.Element("Name").Attribute("update")
                            where insert == null || (bool)insert  && update == null || (bool)update
                            select nameTag.Element("Name").Value;

Name、FirstName、LastNameを与える

誰かがここで私を助けてくれますか?

よろしく。

4

2 に答える 2

0

whereパーツを次のように変更してみてください。

where (insert == null || Convert.ToBoolean(insert.Value))

編集<Name insert='fasle'テストまたはタイプミスに必要かどうかわかりません。

于 2013-03-06T13:27:37.510 に答える
0

属性を null 許容ブール値にキャストします。属性が欠落している場合は、null が返されます。次に、insert値がない (属性が欠落している) かどうか、またはその値がtrue次のとおりであるかどうかを確認します。

var insertTags = from name in xml.Descendants("Name")
                 let insert = (bool?)name.Attribute("insert") 
                 where !insert.HasValue || insert.Value
                 select (string)name;

注意: insert変数には型がありますが、ここでは型Nullable<bool>ではありませんXAttribute


XPath と同じ:

var insertTags = xml.XPathSelectElements("//Name[@insert='true'or not(@insert)]")
                    .Select(n => (string)n);
于 2013-03-06T13:37:40.733 に答える