zoukankan      html  css  js  c++  java
  • XML 解析方法(1)

      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;
                
            }  

                }
              }
    做个快乐的自己。
  • 相关阅读:
    OpenID Connect 验证
    升级到 .NET Core 3.1
    深入 .NET Core 基础
    依赖注入在 dotnet core 中实现与使用:3 使用 Lazy<T> 延迟实例化
    依赖注入在 dotnet core 中实现与使用:2 使用 Extensions DependencyInjection
    依赖注入在 dotnet core 中实现与使用:1 基本概念
    ng-bootstrap 组件集中 tabset 组件的实现分析
    一笔百亿美元军方订单,引发了美国科技公司大混战
    4种事务的隔离级别,InnoDB怎样巧妙实现?
    Google I/O 官方应用中的动效设计
  • 原文地址:https://www.cnblogs.com/Jessy/p/2320861.html
Copyright © 2011-2022 走看看