【问题标题】:XML parse check if attribute existXML解析检查属性是否存在
【发布时间】:2012-11-12 10:34:17
【问题描述】:

我创建了一个方法来检查 XML 文件中是否存在属性。如果它不存在,则返回“False”。它可以工作,但解析文件需要很长时间。它似乎读取每一行的整个文件。我在这里错过了什么吗?我可以让它更有效吗?

    public static IEnumerable<RowData> getXML(string XMLpath)
    {
        XDocument xmlDoc = XDocument.Load("spec.xml");

        var specs = from spec in xmlDoc.Descendants("spec")
                    select new RowData
                    {
                        number= (string)spec.Attribute("nbr"),
                        name= (string)spec.Attribute("name").Value,
                        code = (string)spec.Attribute("code").Value,
                        descr = (string)spec.Attribute("descr").Value,
                        countObject = checkXMLcount(spec),


        return specs;
    }

    public static string checkXMLcount(XElement x)
    {
        Console.WriteLine(x.Attribute("nbr").Value);
        Console.ReadLine();
        try
        {
            if (x.Attribute("mep_count").Value == null)
            {
                return "False";
            }
            else
            {
                return x.Attribute("mep_count").Value;
            }
        }
        catch
        {
            return "False";
        }
    }

我测试了用只返回和接收字符串的方法替换该方法:

public static string checkXMLcount(string x)
{
    Console.WriteLine(x);
    Console.ReadLine();
    return x;

}

我制作了一个只有一行的 XML 文件。控制台打印出该值 15 次。有什么想法吗?

【问题讨论】:

  • 我想知道为什么要编写自己的 XPath 版本?

标签: c# xml linq linq-to-xml


【解决方案1】:

解决了!不需要额外的方法:

countObject = spec.Attribute("mep_count") != null ? spec.Attribute("mep_count").Value : "False",

【讨论】:

  • 如果你有很多这些......你可以封装....... private string SafeAttributeValue(XAttribute xattr) { string returnValue = string.Empty; if (null != xattr) { returnValue = (string)xattr.Value; } 返回返回值; }
【解决方案2】:

你可以试试这个,看看有没有改善

class xmlAttributes
{
    public string Node;
    public Dictionary<string, string> Attributes;
} 

现在使用此 LINQ,所有属性都存储在字典中(每个节点),并且可以通过属性名称访问。

var Result = XElement.Load("somedata.xml").Descendants("spec")
                      .Select(x => new xmlAttributes
                      {
                          Node = x.Name.LocalName,
                          Attributes = x.Attributes()
                                     .ToDictionary(i => i.Name.LocalName,
                                                        j => j.Value)
                      });

检查一个属性是否存在于所有 XML 节点上

var AttributeFound = Result.All(x => x.Attributes.ContainsKey("AttrName"));

检查属性是否至少出现一次

var AttributeFound = Result.Any(x => x.Attributes.ContainsKey("AttrName"));

【讨论】:

    【解决方案3】:

    只是想指出:

    countObject = spec.Attribute("mep_count")?.Value;
    

    这在整个链条上都有效:

    countObject = spec?.Attribute("mep_count")?.Value
    

    这将产生与将 countObject 设置为 null 或该值(如果存在)相同的效果。

    【讨论】:

      猜你喜欢
      • 2018-09-27
      • 1970-01-01
      • 2020-06-09
      • 2013-03-12
      • 1970-01-01
      • 2017-09-25
      • 2023-04-03
      • 2015-06-27
      • 1970-01-01
      相关资源
      最近更新 更多