2

I want to select SMS section block according to tip attribute of SMS xml. Currently: ConfigurationManager.GetSection("Logger/Sms") works but is there any way to get section like 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>
uzay95
  • 16,052
  • 31
  • 116
  • 182
  • 2
    one trick that I use is to just grab the entire section... then deserialize the xml into an object.. then you can just use the object to get your config items and you don't have to mess with that crazy syntax. – John Sobolewski Mar 28 '12 at 12:19
  • Are you sure this works ? I get `Sections must only appear once per config file. See the help topic for exceptions.` error when I try to fetch this from `web.config` or `app.config` files. Can you update the question to include instructions on how to reproduce this ? – AYK Apr 17 '12 at 10:32

1 Answers1

0

You've probably long since moved on, but I created an XPath lookup for xml XElement recently available here: https://github.com/ChuckSavage/XmlLib/ If you want to use jsobo's comment to get the information you want.

You would use it like:

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

By using it with string.Format() syntax, you pass the type to the XPath as well, if you wanted to do a DateTime check, etc. I also find it easier for variable injections as well, instead of "//Sms[@tip='" + variable + "']". XPathElement is just XPath().FirstOrDefault() to return a single element.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69