views:

48

answers:

1

In my program I get passed some XML. If values in this XML fulfil a user defined criteria I store the xml otherwise it gets discarded. The problem I have is that I need to be able to allow the user to define the criteria (also combining multiple element with “OR” and “AND”) and then applying this when I get the XML. This is a C# application, can anyone recommend a library, tool or help in which way I should go about resolving this problem? Shown below is the XML that I will receive. The user may only want to store this if <unit> =1 AND (the first part of <data> = Z OR <data> has ABC after the second coma).

<interface>
  <mac>12345</mac>
  <device>DeviceTypeA</device>
  <id>TestUnit</id>
  <data>
    <unit>1</unit>
    <transaction>
      <event>0</event>
      <data>Z,0,ABC,1234</data>
      <time>2010-06-29T11:33:44.0000000Z</time>
    </transaction >
  </data>
</interface>
A: 

Do your users get to see the XML at all? If so, you could simply allow the user to input an XPath expression, such as

/interface/data/unit=1

or

substring-before(',',/interface/data/transaction/data)='Z'

then simply do

if (xml.SelectNodes(xPathExpression) == null) /*discard*/

IMHO anyone who works with XML should be encouraged to learn XPath; you could provide a few simple examples next to the input to help.

If your users don't see the XML, you're probably better off having a few predefined conditions that the user can select from and then supply a value, otherwise you're going to have to create a whole expression parser, which is probably overkill for a task like this.

Flynn1179