【问题标题】:Querying Linq to xml with an incomplete value使用不完整的值查询 Linq to xml
【发布时间】:2016-04-21 22:54:22
【问题描述】:

我有一个关于查询 Linq 的问题。

例如我有这个 xml:

<output>
    <meta>
        <generated_at>2009-06-19T16:18:40+00:00</generated_at>
        <total_entries>1234</total_entries>
    </meta>
    <entries>
        <entry>
            <url><![CDATA[http://www.example.com/]]></url>
            <phish_id>123456</phish_id>
            <phish_detail_url>http://www.phishtank.com/phish_detail.php?phish_id=123456</phish_detail_url>
            <details>
                <detail>
                    <ip_address>1.2.3.4</ip_address>
                    <cidr_block>1.2.3.0/24</ip_address>
                    <announcing_network>1234</announcing_network>
                    <rir>arin</rir>
                    <detail_time>2009-06-20T15:37:31+00:00</detail_time>
                </detail>
            </details>
            <submission>
                <submission_time>2009-06-19T15:15:47+00:00</submission_time>
            </submission>
            <verification>
                <verified>yes</verified>
                <verification_time>2009-06-19T15:37:31+00:00</verification_time>
            </verification>
            <status>
                <online>yes</online>
            </status>
            <target>1st National Example Bank</target>
        </entry>
        ...
    </entries>
</output>

如果想要获取值为“example”或其他不完整值的“entry”元素,我该怎么做?

谢谢

【问题讨论】:

  • 你可能想使用字符串方法 Contains()。

标签: c# xml linq


【解决方案1】:
var example = XDocument.Parse(inStr)
                   .Descendents("entry")
                   .Where(e => e.Element("url").Value.Contains("example"))
                   .FirstOrDefault();

如果有多个可能的元素,那么您可能不需要 .FirstOrDefault()

【讨论】:

  • 非常感谢。这是完美的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多