正在获取xml属性的节筛选

本文关键字:筛选 属性 xml 获取 | 更新日期: 2023-09-27 18:29:19

我想根据SMS xml的tip属性选择SMS部分块。目前:ConfigurationManager.GetSection("Logger/Sms")有效,但有没有办法获得类似ConfigurationManager.GetSection("Logger/Sms[@tip='VF']")的部分?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="Logger">
      <section name="Sms" type="caSectionTest.LogHandler, caSectionTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </sectionGroup>
  </configSections>
  <Logger>
    <Sms tip="Clickatell">
      <icerik>Soğuk zincir uygulamasından gönderilen sms</icerik>
      <telNo>9053123123123</telNo>
      <api>3363050</api>
      <user>pkUser</user>
      <pass>passhm</pass>
    </Sms>
    <Sms tip="Vodafone">
      <icerik>write something into sms</icerik>
      <telNo>905123123123</telNo>
      <originator>336123</originator>
      <user>ctUser</user>
      <pass>9Mdfpass</pass>
    </Sms>
  </Logger>
</configuration>

正在获取xml属性的节筛选

您可能早就离开了,但我为xml XElement创建了一个XPath查找,最近在这里可用:https://github.com/ChuckSavage/XmlLib/如果你想使用jsobo的评论来获得你想要的信息。

你会像这样使用它:

XElement root = XElement.Load(file);
XElement sms = root.XPathElement("//Sms[@tip={0}]", "VF"); // or "//Sms[@tip='VF']"

通过将其与字符串一起使用。Format()语法,如果你想进行DateTime检查等,你也可以将类型传递给XPath。我还发现变量注入也更容易,而不是"//Sms[@tip='" + variable + "']"。XPathElement只是返回单个元素的XPath().FirstOrDefault()