質問

親ノード「委員会」を持つXML文字列があり、その別の子ノード「委員会」の内部にあります。私が使用しているとき」from committee in xDocument.DescendantsAndSelf("Committee")「子育ても読んでいますが、子ノードを読みたくありません。親ノードだけを読みたいだけです。

<Committee>
  <Position>STAFF</Position>
  <Appointment>1/16/2006</Appointment>
  <Committee>PPMSSTAFF</Committee>
  <CommitteeName>PPMS Staff</CommitteeName>
  <Expiration>12/25/2099</Expiration>      
</Committee>
<Committee>
   <Position>STAFF</Position>
  <Appointment>4/16/2004</Appointment>
  <Committee>PMOSSTAFF</Committee>
  <CommitteeName>PPMS </CommitteeName>
  <Expiration>12/25/2099</Expiration>
</Committee>

     XElement xDocument= XElement.Parse(xml);

 var committeeXmls = from Committee in xDocument.Descendants("Committee")
                                select new
                                {
                                    CommitteeName = Committee.Element("CommitteeName"),
                                    Position = Committee.Element("Position"),
                                    Appointment = Committee.Element("Appointment"),
                                    Expiration = Committee.Element("Expiration")
                                };

            int i = 0;
            foreach (var committeeXml in committeeXmls)
            {
                if (committeeXml != null)
                {
                    drCommittee = dtCommittee.NewRow();
                    drCommittee["ID"] = ++i;
                    drCommittee["CommitteeName"] = committeeXml.CommitteeName.Value;
                    drCommittee["Position"] = committeeXml.Position.Value;
                    drCommittee["Appointment"] = committeeXml.Appointment.Value;
                    drCommittee["Expiration"] = committeeXml.Expiration.Value;

                    dtCommittee.Rows.Add(drCommittee);                                        //   educationXml.GraduationDate.Value, educationXml.Major.Value);
                }
            }

正しい解決策はありません

他のヒント

使用 Elements の代わりに Descendants.

これを変える:

from Committee in xDocument.Descendants("Committee")

これに:

from Committee in xDocument.Elements("Committee")

これは子供を返します Committee 現在の要素の要素(xDocument 変数)。

XpathSelectelements Extensionメソッド(System.XML.XPath名空間)を使用して、委員会の子要素を持つ委員会の要素のみを選択できます。

var committeeXmls = from Committee in xDocument.XPathSelectElements("Committee[Committee]")
                    ...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top