XML解析检查属性是否存在

时间:2021-12-30 15:30:41

I've made a method which checks if an attribute exist in a XML-file. If it does not exist it returns "False". It works but it takes a very long time to parse the file. It seems it reads the whole file for each single row. Have I missed something here? Can I make it more effective somehow?

我已经创建了一个方法来检查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";
        }
    }

I tested to replace the method with one that only returns and receive string:

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

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

}

I made a XML-file with only one single row. The console prints out the value 15 times. Any ideas?

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

2 个解决方案

#1


38  

Solved! No extra method needed:

解决了!无需额外方法:

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

#2


2  

You can try this and see if there is any improvement

您可以尝试这个,看看是否有任何改进

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

Now with this LINQ,all attributes are stored in a dictionary(per Node) and could be accessed through the attribute name.

现在使用此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)
                      });

Checks if an attribute exists on all XML Nodes

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

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

Checks if the attribute appears at least once

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

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

#1


38  

Solved! No extra method needed:

解决了!无需额外方法:

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

#2


2  

You can try this and see if there is any improvement

您可以尝试这个,看看是否有任何改进

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

Now with this LINQ,all attributes are stored in a dictionary(per Node) and could be accessed through the attribute name.

现在使用此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)
                      });

Checks if an attribute exists on all XML Nodes

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

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

Checks if the attribute appears at least once

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

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