XmlQuery Class

MSBuild

Reads a value or values from lines of XML


Namespace: MSBuild.Community.Tasks.Xml
Assembly: MSBuild.Community.Tasks (in MSBuild.Community.Tasks.dll)

Syntax

Visual Basic (Declaration)
Public Class XmlQuery
    Inherits Task
C#
public class XmlQuery : Task
C++
ref class XmlQuery : Task
J#
public class XmlQuery extends Task
JScript
public class XmlQuery extends Task

Example

Read an attribute value by selecting it with an XPath expression:

 Copy Code
<ReadLinesFromFile File="web.config">
    <Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>

<XmlQuery Lines="@(FileContents)"
    XPath = "/configuration/system.web/compilation/@defaultLanguage">
    <Output TaskParameter="Values" PropertyName="CompilationLanguage" />
</XmlQuery>

<Message Text="The default language is $(CompilationLanguage)." />
Read attribute values (from an XML file) using item metadata on a selected element node:
 Copy Code
<XmlQuery XmlFileName="$(MSBuildProjectDirectory)\web.config"
    XPath = "/configuration/system.web/compilation">
    <Output TaskParameter="Values" ItemName="CompilationElement" />
</XmlQuery>

<Message Text="The default language is: $(CompilationElement.defaultLanguage)." />
<Message Text="Debug is enabled: $(CompilationElement.debug)." />
Read an element value (requires use of the reserved metadata property "_value"):
 Copy Code
<ReadLinesFromFile File="web.config">
    <Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>

<XmlQuery Lines="@(FileContents)"
    XPath = "/configuration/singleValue/LastName">
    <Output TaskParameter="Values" PropertyName="LastNameElement" />
</XmlQuery>

<Message Text="The last name is %(LastNameElement._value)" />

Remarks

Use the Lines property (possibly populated from the the ReadLinesFromFile task) if you want to perform multiple queries against some XML in memory. Use the XmlFileName property to query a large XML file.

An XPath expression can return multiple nodes in the Values collection. The number of nodes returned is availabe in the ValuesCount output TaskParameter.

When the XPath expression resolves to an element node, all of the attributes of the element are added as metadata to the returned ITaskItem. In addition, some reserved metadata properties are available on all element nodes. They are all prefixed with the ReservedMetaDataPrefix, which is a single underscore (_) by default.

Reserved Property
_valueThe value of the node (non-xml text between the opening and closing tags).
_innerXmlThe markup representing the children of this node.
_outerXmlThe markup representing this node and all its child nodes.

Inheritance Hierarchy

System.Object
   Microsoft.Build.Utilities.Task
      MSBuild.Community.Tasks.Xml.XmlQuery

Thread Safety

Public static (Shared in Visual Basic)staticShared members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

See Also

Syntax based on .NET Framework version 2.0.
Documentation version 1.0.0.0.