0

1 つの値を取得するために照会および検索したい複雑な XML ファイルが多数あります。以下に 2 つの短い例を示します。

1

<textInfo>
 <freeText>
  <informationType>15</informationType>
 </freeText>
</textInfo>
<textInfo>
 <freeText>
  <textSubject>4</textSubject>
  <informationType>47</informationType>
 </freeText>
  <freeText>My required text</freeText>
</textInfo>
<textInfo>
 <freeText>
  <informationType>733</informationType>
  <status>1</status>
 </freeText>
</textInfo>

2

<textInfo>
  <freeText>
    <textSubject>4</textSubject>
    <informationType>15</informationType>
    <status>0</status>
  </freeText>
</textInfo>
<textInfo>
  <freeText>
    <textSubject>4</textSubject>
    <informationType>47</informationType>
  </freeText>
  <freeText>My required text</freeText>
</textInfo>
<textInfo>
  <freeText>
    <textSubject>4</textSubject>
    <status>0</status>
  </freeText>
</textInfo>
<textInfo>
  <freeText>
    <textSubject>4</textSubject>
    <informationType>61</informationType>
  </freeText>
</textInfo>
<textInfo>
  <freeText>
    <textSubject>4</textSubject>
    <informationType>39</informationType>
  </freeText>
  <freeText>some text</freeText>
  <freeText>some other text</freeText>
</textInfo>

必要な出力: タグ内のテキスト<freeText>(必要なテキスト) タグ<informationType>= (47)

複数のlinq to xmlコードを試しましたが、どれもうまくいきませんでした。元。

コード1

var query = from q in XDocument.Parse(requestInterceptor.LastResponseXML)
    .Descendants("freeText")
    where (bool)q.Element("informationType").Equals("47")
    select q.Element("freeText").Value;

コード 2

XDocument doc = XDocument.Parse(requestInterceptor.LastResponseXML); ;
    var q = doc
    .Descendants("freeText[1]")
    .Where(ft => ((int?)ft.Element("informationType")) == 47);
Object.Type = q.ToString();

コード3

XmlDocument doc = new XmlDocument();
                    doc.LoadXml(requestInterceptor.LastResponseXML);

                    foreach (XmlNode node in doc.DocumentElement.SelectSingleNode("//textInfo/freeText[informationType>=47]/informationType"))
                    {
                        Object.Type = node.InnerText;
                    }

コード4

XmlDocument doc2 = new XmlDocument();
                    doc.LoadXml(requestInterceptor.LastResponseXML);
                    foreach (XmlNode node in doc2.SelectNodes("//textInfo/freeText[informationType>=47]/informationType"))
                    {
                        Object.Type = node.InnerText;
                    }

注:私は webservice から xml のみを取得しています。私はそれをどこかに保存していないか、xmlファイルとして持っています。また、これはxmlの一部であり、情報を取得するだけでよいことに注意してください

4

2 に答える 2