0

私は質問に打たれました。私を助けてください。

私はxmlを持っています

      <Set type="Main">
          <FirstUnit xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
            <CreateDate>2013-06-06T13:19:17.457</CreateDate>
            <PrimaryKey>1</PrimaryKey>
          </FirstUnit>
          <Secondunit  xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
            <CreateDate>2013-06-06T13:19:17.457</CreateDate>
            <PrimaryKey>1</PrimaryKey>
            <Exercise>Test</Exercise>
          </SecondUnit>
          <FirstUnit xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
            <CreateDate>2013-06-06T13:19:17.457</CreateDate>
            <PrimaryKey>2</PrimaryKey>
          </FirstUnit>
          <Secondunit  xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
            <CreateDate>2013-06-06T13:19:17.457</CreateDate>
            <PrimaryKey>2</PrimaryKey>
            <Exercise>Test</Exercise>
          </SecondUnit>
     </Set>

ここで必要なのは、主キーに基づいてユニットをグループ化することだけです。つまり、FirstUnit と SecondUnit は<Primarykey>、あるグループではノード値が「1 PrimaryKey」で、別のグループではノード値が「2」である必要があります。

次のクエリで試してみましたが、さらに絞り込む必要があります。

var elements = xDocument.GroupBy(a => a.Elements().Descendants().Where(x => x.Name.LocalName == "PrimaryKey" ).ToList());

前もって感謝します。

4

1 に答える 1

1

これらの要素のでグループ化する必要があるように私には思えます:

// If <Set> is the document element, change Descendants("Set") to Root
var elements = xDocument.Descendants("Set")
                        .Elements()
                        .GroupBy(x => (int) x.Element("PrimaryKey"));

(必要に応じて、要素の名前空間を指定します。Where句を使用してローカル名だけをチェックするのは少し面倒です。)

これがうまくいかない場合は、何をしようとしているのかについて詳しく教えてください。

于 2013-06-21T10:54:54.083 に答える