/// <summary> /// get-item cmdlet callback /// </summary> /// <param name="path"></param> protected override void GetItem(string path) { WriteVerbose(string.Format("XmlItemProvider::GetItem(Path = '{0}')", path)); string npath = XmlProviderUtils.NormalizePath(path); string xpath = XmlProviderUtils.PathNoDrive(npath); XmlDriveInfo drive = XmlProviderUtils.GetDriveFromPath(path, base.ProviderInfo); if (drive == null) { ErrorRecord error = new ErrorRecord(new InvalidProgramException("Unable to retrieve the drive for this path"), "drive", ErrorCategory.InvalidData, null); WriteError(error); } XmlDocument xml = drive.XmlDocument; XmlNodeList nodes = xml.SelectNodes(xpath, drive.NamespaceManager); // ------------------------------ // NOTE: We could throw an ItemNotFoundException here if the nodelist returned is null // or empty. In this case I decided not to because the fact that get-item returns // nothing indicates that case. For other operations such as clear-item or set-item // it is a good idea to throw that exception to indicate specifically why you're unable to // perform the action. // ------------------------------ foreach (XmlNode node in nodes) { WriteItemObject(node, path, false); } }
private XmlNodeList GetXmlNodesFromPath(string path) { string npath = XmlProviderUtils.NormalizePath(path); string xpath = XmlProviderUtils.PathNoDrive(npath); XmlDriveInfo drive = XmlProviderUtils.GetDriveFromPath(path, base.ProviderInfo); if (drive == null) { ErrorRecord error = new ErrorRecord(new InvalidProgramException("Unable to retrieve the drive for this path"), "drive", ErrorCategory.InvalidData, null); WriteError(error); return(null); } XmlDocument xml = drive.XmlDocument; return(xml.SelectNodes(xpath)); }
/// <summary> /// test-path cmdlet callback /// </summary> /// <param name="path"></param> /// <returns></returns> protected override bool ItemExists(string path) { WriteVerbose(string.Format("XmlItemProvider::ItemExists(Path = '{0}')", path)); string npath = XmlProviderUtils.NormalizePath(path); string xpath = XmlProviderUtils.PathNoDrive(npath); XmlDriveInfo drive = XmlProviderUtils.GetDriveFromPath(path, base.ProviderInfo); if (drive == null) { return(false); } XmlDocument xml = drive.XmlDocument; if (xml.SelectSingleNode(xpath, drive.NamespaceManager) == null) { return(false); } else { return(true); } }