Reputation: 9279
I've started working with a new database that encodes certain values as XML strings. Invariably all I need is the value of an attribute in one of the elements, but it's a different attribute for different columns in the db. Here's an example of one of them...
<X C="1" I="0">
<E D="1000Y0M0W0D" P="1" A="5700" />
</X>
And in this case, all I need is the value of the A.
Is this something I can do easily using System.XML, or should I just use RegEx or Split on the string?
Upvotes: 0
Views: 39
Reputation: 633
It's always good to use a tool that was created for a particular purpose. In this case, System.Xml
. There are several reasons: first of all, it takes a lot of workload away from you (the parsing in this case). Second, it's optimized on a level which you won't be able to do yourself, and will work faster than any custom implementation you would create (Of course, for a simple xml this may not be completely true, but it is for the general case). The architecture is also constructed that it will give you very good feedback in case of errors or invalid xmls; also, accessing an attribute or element is just a simple method call.
What if your requirement changes in the future, to get something else than A
? Or what if your xml structure gets more complicated? Using a specialized tool will limit the amount of code changes that you need to make in such cases.
Upvotes: 1