开发者

expression builder for XML

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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜