public static string TryGetElementValue(this XElement parentEl, string elementName, string defaultValue = null)
        {
            var foundEl = parentEl.Element(elementName);
            if (foundEl != null)
            {
                return foundEl.Value;
            }
            else
            {
                return defaultValue;
  public static string TryGetElementAttribute(this XElement element, string attributeName, string defaultValue = null)
        {           
                var foundAttr = element.Attribute(attributeName);
                if (foundAttr != null)
                    return foundAttr.Value;
                else
                    return defaultValue;
          
        }

        public static string TryGetElementAttribute(this XElement parentEl, string elementName, string attributeName, string defaultValue = null)
        {
            var foundEl = parentEl.Element(elementName);
            if (foundEl != null)
            {
                var foundAttr = foundEl.Attribute(attributeName);
                if (foundAttr != null)
                    return foundAttr.Value;
                else
                    return defaultValue;
            }
            else
            {
                return defaultValue;
            }
        }

        public static string TryGetElementValueByAttribute(this XElement parentEl, string elementName, string attributeName, string defaultValue = null)
        {
            string retVal = defaultValue;
            if (parentEl.HasElements) {
                foreach (var element in parentEl.Descendants()) {
                   
                    var foundAttr = element.Attribute("name");
                    if (foundAttr != null && foundAttr.Value == attributeName)
                    {
                        retVal = element.Value;
                        break;                        
                    }                       
                }
            }
            return retVal;
            
        }  

            }
          }

相关文章:

  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2021-05-08
  • 2022-12-23
  • 2021-12-06
猜你喜欢
  • 2021-06-28
  • 2021-11-18
  • 2022-01-28
  • 2021-10-04
  • 2021-11-27
  • 2021-11-04
  • 2021-08-14
相关资源
相似解决方案