Element Resource
Synopsis
Manages XML element content and attributes using XPath expressions. Supports
creating,
modifying, and removing elements. Parent elements are created recursively when
they don't exist.
Uses the hybrid pattern with both _exist (for the element) and _purge (for
attributes).
Type
Capabilities
- Get
- Set
- Delete
Properties
path
Absolute file path to the XML document.
xPath
XPath expression to locate the element.
value
Text content (inner text) of the element.
attributes
Attributes to set on the element as key-value pairs.
namespaces
Namespace prefix mappings for XPath evaluation.
_purge
When true, removes attributes not in the list. When false (default), only
adds/updates.
_exist
Whether the element should exist. Defaults to true.
Examples
Example 1 — Get an XML element
Example 2 — Set element attributes
Example 3 — Set element value with namespace
Example 4 — Delete an element
Example 5 — Configuration document
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Set application name
type: OpenDsc.Xml/Element
properties:
path: C:\inetpub\wwwroot\web.config
xPath: "//configuration/appSettings/add[@key='AppName']"
attributes:
key: AppName
value: MyApplication
- name: Set connection string
type: OpenDsc.Xml/Element
properties:
path: C:\inetpub\wwwroot\web.config
xPath: "//configuration/connectionStrings/add[@name='Default']"
attributes:
name: Default
connectionString: "Server=db;Database=app;Integrated Security=true"
_purge: true
Exit codes
| Code | Description |
|---|---|
| 0 | Success |
| 1 | Error |
| 2 | Invalid JSON |
| 3 | XML file not found |
| 4 | Invalid XML |
| 5 | Invalid XPath expression |
| 6 | Invalid argument |
| 7 | IO error |