{"record":{"id":"865931e6c34b806c","repo":"dotnet/reactive","slug":"did-not-find-xpath","errorCode":null,"errorMessage":"Did not find '{xpath}'","messagePattern":"Did not find '(.+?)'","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Test/Gauntlet/RxGauntlet.Common/Xml/XmlExtensions.cs","lineNumber":14,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information.\n\nusing System.Xml;\n\nnamespace RxGauntlet.Xml;\n\npublic static class XmlExtensions\n{\n    public static XmlNode GetRequiredNode(this XmlDocument document, string xpath)\n    {\n        return document.SelectSingleNode(xpath)\n            ?? throw new InvalidOperationException($\"Did not find '{xpath}'\");\n    }\n\n    public static void SetAttribute(this XmlNode node, string attributeName, string attributeValue)\n    {\n        var attributes = node.Attributes!;\n        if (attributes[attributeName] is not XmlAttribute attribute)\n        {\n            attribute = node.OwnerDocument!.CreateAttribute(attributeName);\n            attributes.Append(attribute);\n        }\n\n        attribute.Value = attributeValue;\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Test/Gauntlet/RxGauntlet.Common/Xml/XmlExtensions.cs#L1-L29","documentation":"GetRequiredNode is an XmlDocument extension that selects a single node via XPath and throws InvalidOperationException if SelectSingleNode returns null. It exists so tooling can fail fast when an expected element/attribute is missing from an XML document rather than leaking a NullReferenceException later.","triggerScenarios":"Calling document.GetRequiredNode(\"/some/xpath\") where no node matches the XPath — wrong path spelling, wrong namespace prefix, or the element genuinely absent from the document.","commonSituations":"Gauntlet build scripts expecting a project-file element (e.g. a property in a .csproj) that was renamed or removed; XML documents with default namespaces where the XPath lacks namespace handling.","solutions":["Verify the XPath string against the actual document structure and fix typos/case","Handle XML namespaces explicitly (add namespace manager or use local-name() in the XPath)","Confirm the source document is the expected version/schema that contains the node"],"exampleFix":"// before\nvar node = doc.GetRequiredNode(\"/Project/PropertyGroup/TargetFramework\");\n// after\nvar node = doc.SelectSingleNode(\"/Project/PropertyGroup/TargetFramework\");\nif (node == null) { /* document lacks the element; handle or report clearly */ }","handlingStrategy":"validation","validationCode":"var node = document.SelectSingleNode(xpath);\nif (node is null) { Console.WriteLine($\"Document lacks node for xpath '{xpath}'\"); return; }","typeGuard":"bool hasNode = document.SelectSingleNode(xpath) is not null;","tryCatchPattern":"try { var n = doc.GetRequiredNode(xpath); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Did not find\")) { Console.Error.WriteLine($\"XML missing expected node: {ex.Message}\"); }","preventionTips":["Validate XPath expressions against a sample of the real document in tests","Account for XML namespaces in your XPath (use local-name() or a namespace manager)","Pin/verify the input document schema version before processing"],"tags":["csharp","xml","xpath","not-found"],"backgroundTag":"resource-not-found","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}