/// <summary> /// Gets the node value with the xml path. /// </summary> /// <typeparam name="T">Type of the value.</typeparam> /// <param name="xmlPath">The XML path.</param> /// <param name="defaultValue">The default value.</param> /// <param name="defaultOnError">When convert failed, true to return a default value of targetType; false to return defaultValue.</param> /// <returns>The value.</returns> public T NodePathValue <T>(string xmlPath, T defaultValue, bool defaultOnError = false) { if (this.IsNullOrWhiteSpace(xmlPath)) { return(defaultValue); } if (!xmlPath.Contains(".")) { return(XmlConverter.ToObject <T>(this.GetValueWithIndex(xmlPath), defaultValue, defaultOnError)); } else { var propertyChain = xmlPath.Trim().Split('.'); FluentXml result = this; foreach (var propertyName in propertyChain) { if (!propertyName.EndsWith("]")) { result = result.Node(propertyName); } else { return(XmlConverter.ToObject <T>(result.GetValueWithIndex(propertyName), defaultValue, defaultOnError)); } } return(XmlConverter.ToObject <T>(result.Value(), defaultValue, defaultOnError)); } }
/// <summary> /// Gets the node value with the xml path. /// </summary> /// <example>example: xxx.NodePathValue("A.B.C") or xxx.NodePathValue("A.B.C[D]")</example> /// <param name="xmlPath">The XML path.</param> /// <returns>The value.</returns> public string NodePathValue(string xmlPath) { if (this.IsNullOrWhiteSpace(xmlPath)) { return(null); } if (!xmlPath.Contains(".")) { return(this.GetValueWithIndex(xmlPath)); } else { var propertyChain = xmlPath.Trim().Split('.'); FluentXml result = this; foreach (var propertyName in propertyChain) { if (!propertyName.EndsWith("]")) { result = result.Node(propertyName); } else { return(result.GetValueWithIndex(propertyName)); } } return(result.Value()); } }