2

我想根据tipSMS xml 的属性选择 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>
4

1 回答 1

0

您可能早就继续前进了,但我最近在这里为 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']"

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

于 2012-06-04T22:57:39.553 回答