/// <summary> /// 获取指定节点的指定值 /// </summary> /// <param name="node">Xml节点</param> /// <param name="xmlValueType">值类型</param> /// <param name="defaultValue">默认值</param> /// <returns>值</returns> public static string GetXmlNodeValue(XmlNode node, XmlValueType xmlValueType, string defaultValue) { string nodeValue = defaultValue; if (node != null) { try { switch (xmlValueType) { case XmlValueType.Value: nodeValue = node.Value; break; case XmlValueType.InnerText: nodeValue = node.InnerText; break; case XmlValueType.InnerXml: nodeValue = node.InnerXml; break; case XmlValueType.OuterXml: nodeValue = node.OuterXml; break; } } catch { nodeValue = defaultValue; } } return(nodeValue); }
public XmlMap(string _rootPath, XmlKeyType _keyType, string _keyPath, XmlValueType _valueType, string _valuePath) { rootPath = _rootPath; keyType = _keyType; keyPath = _keyPath; valueType = _valueType; valuePath = _valuePath; }
/// <summary> /// 通过xPath查询相应的值 /// </summary> /// <param name="node">Xml节点</param> /// <param name="xPath">xPath查询</param> /// <param name="xmlValueType">值类型</param> /// <returns>值</returns> public static string GetXmlNodeValue(XmlNode node, string xPath, XmlValueType xmlValueType) { if (xPath == "") { return(null); } else { return(GetXmlNodeValue(node.SelectSingleNode(xPath), xmlValueType, null)); } }
/// <summary> /// Change the type of the internal representation of the IXmlValue. /// </summary> /// <remarks> /// A failed conversion will leave the value as <c>null</c>. /// </remarks> /// <param name="type"> /// The enumerated type to convert to. /// </param> /// <returns> /// The current value of this SimpleValue object as the specified /// type or <c>null</c>. /// </returns> protected virtual object EnsureType(XmlValueType type) { object oldValue = InternalValue; object newValue = Convert(oldValue, type); if (oldValue != newValue && IsMutable) { InternalValue = newValue; } return(newValue); }
/// <summary> /// 获取指定的多个节点的指定值 /// </summary> /// <param name="node">Xml节点</param> /// <param name="xPath">xPath查询</param> /// <param name="xmlNamespace"></param> /// <param name="xmlValueType">值类型</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static List <string> GetXmlNodesValue(XmlNode node, string xPath, XmlNamespaceManager xmlNamespace, XmlValueType xmlValueType, string defaultValue) { var result = new List <string>(); if (String.IsNullOrWhiteSpace(xPath)) { return(result); } var subNodeList = node.SelectNodes(xPath, xmlNamespace)?.Cast <XmlNode>().ToList(); if (subNodeList == null || !subNodeList.Any()) { return(result); } result.AddRange(subNodeList.Select(subNode => GetXmlNodeValue(subNode, xmlValueType, defaultValue))); return(result); }
/// <summary> /// 通过xPath查询相应的值 /// </summary> /// <param name="node">Xml节点</param> /// <param name="xPath">xPath查询</param> /// <param name="xmlNamespace"></param> /// <param name="xmlValueType">值类型</param> /// <param name="defaultValue">默认值</param> /// <returns>值</returns> public static string GetXmlNodeValue(XmlNode node, string xPath, XmlNamespaceManager xmlNamespace, XmlValueType xmlValueType, string defaultValue) { if (xPath == "") { return(defaultValue); } else { return(GetXmlNodeValue(node.SelectSingleNode(xPath, xmlNamespace), xmlValueType, defaultValue)); } }
/// <summary> /// 获取指定节点的指定值 /// </summary> /// <param name="node">Xml节点</param> /// <param name="xmlValueType">值类型</param> /// <returns>值</returns> public static string GetXmlNodeValue(XmlNode node, XmlValueType xmlValueType) { return(GetXmlNodeValue(node, xmlValueType, null)); }
/// <summary> /// 获取指定的多个节点的指定值 /// </summary> /// <param name="node">Xml节点</param> /// <param name="xPath">xPath查询</param> /// <param name="xmlValueType">值类型</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static List <string> GetXmlNodesValue(XmlNode node, string xPath, XmlValueType xmlValueType, string defaultValue) { return(GetXmlNodesValue(node, xPath, null, xmlValueType, defaultValue)); }
/// <summary> /// Gets a value indicating whether the <see cref="XmlValue"/> object contains /// the requested type. /// </summary> /// <param name="valueType">The <see cref="XmlValueType"/> to check against.</param> /// <returns> /// Returns <c>true</c> if the <see cref="XmlValue"/> /// is the requested type. /// </returns> public bool IsType(XmlValueType valueType) { return(true); }
/// <summary> /// Initializes a new instance of the XmlValue class using the specified <see cref="XmlData"/> and data type. /// </summary> /// <param name="valueType">The specific value type to initialize with.</param> /// <param name="data">The <see cref="XmlData"/> representation /// of the value type.</param> public XmlValue(XmlValueType valueType, XmlData data) { }
/// <summary> /// Initializes a new instance of the XmlValue class using the specified data and data type. /// <see cref="System.String"/> representation of that value. /// </summary> /// <param name="valueType">The specific value type to initialize with.</param> /// <param name="data">The <see cref="System.String"/> representation of the value type.</param> public XmlValue(XmlValueType valueType, string data) { }
/// <summary> /// Convert the passed object to the specified type. /// </summary> /// <param name="o"> /// The object value. /// </param> /// <param name="type"> /// The enumerated type to convert to. /// </param> /// <returns> /// An object of the specified type. /// </returns> protected virtual object Convert(object o, XmlValueType type) { return(XmlHelper.Convert(o, type)); }
// ----- IParameterResolver methods --------------------------------- /// <summary> /// Attempt to resolve the provided macro in the format of <code>{user-defined-name [default-value]}</code> /// against the attributes provided at construction time. /// </summary> /// <param name="type">parameter type</param> /// <param name="value">the raw marco</param> /// <returns>the resolved value converted to the appropriate type</returns> /// <exception cref="ArgumentException"> /// If the provided macro value is in the incorrect format. /// </exception> /// <exception cref="ArgumentException"> /// The parameter cannot be resolved against the attributes provided at construction time and the /// macro does not include a default /// </exception> /// <exception cref="ArgumentNullException"> /// If <code>type</code> or <code>value</code> is <code>null</code> /// </exception> public virtual object ResolveParameter(string type, string value) { if (type == null) { throw new ArgumentNullException("type cannot be null"); } if (value == null) { throw new ArgumentNullException("value cannot be null"); } IDictionary attributes = m_attributes; value = value.Trim(); type = type.Trim(); if (value.IndexOf('{') != 0 || value.IndexOf('}') == value.Length - 2) { throw new ArgumentException(String.Format("The specified macro parameter '{0}' is invalid", value)); } string rawParameter = value.Substring(1, value.Length - 2).Trim(); int defaultStart = rawParameter.IndexOf(' '); string name = defaultStart == -1 ? rawParameter : rawParameter.Substring(0, defaultStart).Trim(); string defaultValue = defaultStart == -1 ? null : rawParameter.Substring(defaultStart + 1).Trim(); object resolvedValue = attributes[name]; if (resolvedValue == null) { if (defaultValue == null) { throw new ArgumentException(String.Format("The specified parameter name '{0}' in the macro " + "parameter '{1}' is unknown and not resolvable", name, rawParameter)); } resolvedValue = defaultValue; defaultValue = null; } if (resolvedValue != null) { XmlValueType expectedType = XmlHelper.LookupXmlValueType(type); if (expectedType == XmlValueType.Unknown) { throw new ArgumentException("Unknown type: " + type); } object converted = XmlHelper.Convert(resolvedValue, expectedType); if (converted == null && defaultValue != null && expectedType != XmlValueType.Xml) { converted = XmlHelper.Convert(defaultValue, expectedType); } if (converted != null) { return(converted); } } return(null); }