我有许多复杂的 XML 文件,我想查询和搜索以获得一个值,下面是两个缩短的示例:
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>
(我需要的文本) where tag <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;
}
注意:我只从 web 服务获取 xml。我没有将它存储在某处或将其作为 xml 文件。另请注意,这是 xml 的一部分,只有我需要获取信息