---
source_files:
- Common/DTS.Common/XMLUtils/DTSXMLFile.cs
- Common/DTS.Common/XMLUtils/NullableElement.cs
- Common/DTS.Common/XMLUtils/TestMetadataXml.cs
generated_at: "2026-04-16T02:55:32.255842+00:00"
model: "Qwen/Qwen3-Coder-Next-FP8"
schema_version: 1
sha256: "efe0fd52c00cf697"
---
# XMLUtils
## Documentation: `DTS.Common.XMLUtils` Module
---
### 1. Purpose
This module provides utility functions for parsing, extracting, and aggregating XML metadata from DTS test files. It supports reading attribute values from XML files (e.g., `TotalItems`), extracting inner XML content from an `XmlReader`, and consolidating multiple `.dts` XML test files into a unified `XDocument` structure for downstream processing (e.g., test execution or reporting). The module exists to abstract low-level XML handling logic and ensure consistent parsing behavior across the codebase.
---
### 2. Public Interface
#### `DTSXMLFile.GetItemsToCompleteCount(string fileName)`
```csharp
public static double GetItemsToCompleteCount(string fileName)
```
- **Behavior**: Loads the XML node from `fileName` using `FileUtils.GetImportXmlNode`, then extracts the `TotalItems` attribute value as a `double`. Returns `0.0` if the attribute is missing, invalid, or any exception occurs during parsing.
- **Note**: Uses `CultureInfo.InvariantCulture` for conversion.
#### `DTSXMLFile.GetInnerXML(XmlReader reader)`
```csharp
public static string GetInnerXML(XmlReader reader)
```
- **Behavior**: Reads the current element’s inner XML text content. Assumes the reader is positioned on the start element. Advances the reader past the end element. Returns the text value of the element (or empty string if none). Throws `ArgumentException` if `reader` is `null`.
- **Implementation details**:
- Calls `reader.ReadStartElement(reader.Name)` to consume the start element.
- Reads `reader.Value` (which may be empty for non-text nodes).
- If the current node is `XmlNodeType.Text`, advances once more.
- Calls `reader.ReadEndElement()` to consume the end element.
#### `TestMetadataXml.GetTestMetadataXml(string path, string file = "", string pattern = "")`
```csharp
public static XDocument GetTestMetadataXml(string path, string file = "", string pattern = "")
```
- **Behavior**: Populates `FileUtils.FileList` with files matching `pattern` (default `".dts"`) under `path`, or adds a single `file` if provided. Then calls `SetTestMetadataType` to process the list and return a consolidated `XDocument`.
- **Output structure**:
```xml
...
```
- **Error handling**: Logs exceptions per-file via `APILogger.Log` and continues processing remaining files. Returns an empty `XDocument` on total failure.
#### `TestMetadataXml.SetTestMetadataType(IEnumerable fileList)` *(private)*
```csharp
private static XDocument SetTestMetadataType(IEnumerable fileList)
```
- **Behavior**: Core logic for aggregating XML content from `fileList`. For each file:
- Reads entire file content as string.
- Splits content on `"` element exists: adds `DataType` (`"ALL"` if filename contains `"ALL"`, else `"ROI"`), `FilePath`, and `FileDate` (creation time) as attributes.
- Adds `` or `` element to a new `` container (with auto-incremented `Id` attribute).
- Aggregates all `` elements under root ``.
---
### 3. Invariants
- **`DTSXMLFile.GetItemsToCompleteCount`**:
- Returns `0.0` for any failure (missing file, invalid XML, missing/invalid `TotalItems` attribute).
- Does not validate XML structure beyond attribute extraction.
- **`DTSXMLFile.GetInnerXML`**:
- Requires the `XmlReader` to be positioned on a start element (e.g., after `Read()` or `ReadStartElement()`).
- Always advances the reader past the end element.
- Does not handle nested elements or mixed content; only returns the *immediate* text value (`reader.Value`).
- **`TestMetadataXml.GetTestMetadataXml` / `SetTestMetadataType`**:
- `FileUtils.FileList` is mutated as a side effect (overwritten with the processed file list).
- `DataType` is determined solely by whether the filename contains `"ALL"` (case-sensitive).
- `FileDate` uses `File.GetCreationTime(file)` (system-dependent; may be unreliable on some platforms).
- XML fragments in a single file must be well-formed when prefixed with `"` → `Value = ""`). Does *not* return full inner XML (despite the name).
- Assumes the element has no mixed content; text after child elements would be missed.
- **`TestMetadataXml`**:
- **Critical**: Splits XML content on `"` elements, they are added as separate `` children under the same ``.
- No validation of XML structure beyond checking for `` or `` root elements.
- **`NullableElement`**:
- `Value` throws `InvalidOperationException` if accessed when `HasValue == false`.
- `GetValueOrDefault()` returns the *default value of `T`* (e.g., `0` for `int`, `null` for reference types) if no value exists—this may be ambiguous if `T` itself is nullable.
- The `explicit operator T` may cause unexpected exceptions if used on an empty instance.
- **General**:
- No thread-safety guarantees (static fields like `FileUtils.FileList` are shared).
- `SuppressMessage` on `DTSXMLFile` indicates intentional deviation from naming conventions (acronym "DTS" preserved).
---
*Documentation generated from provided source files. No behavior inferred beyond explicit code.*