msbuildmsbuild-taskmsbuild-4.0

XmlPeek empty string causes failure


So in my targets file, I've got a line that looks like this:

<XmlPeek Namespaces="" XmlInputPath="file.xml" Query="/data/@AttributeOne">
  <Output TaskParameter="Result" ItemName="my_AttributeOne" />
</XmlPeek>

in "file.xml", I have:

<data AttributeOne="abc" AttributeTwo="def" />

it also reads a few other attributes.

When the attribute has data, everything works fine... but when I leave AttributeOne as an empty string (""), XmlPeek blows chunks with the following error:

The "XmlPeek" task's outputs could not be retrieved from the "Result" parameter. Parameter "includeEscaped" cannot have zero length.

if I remove the attribute ENTIRELY, it works fine (the resulting item is obviously and understandably blank)

The question is... how can I DETERMINE, WITHOUT blowing chunks, the value of a blank attribute... whether by pre-testing for a value, or by correctly handling the blank, or some other means.


CONSTRAINT: the only real requirement is to stick to the built-in tasks (XmlPeek)... I'm aware of XmlRead in the community tasks... for various reasons, I want to use out-of-the-box tasks.

Thanks in advance!


Solution

  • The error happens because an empty string is being used as the Item Identifier. I guess identifiers cannot be the empty string. If you remove the attribute then the result is null and no Item is created so that's why that doesn't throw an error.

    Maybe try return the result as a Property instead of an Item.