您能提高这种 linq-to-xml 方法的性能吗?

发布于 2024-10-24 06:39:58 字数 761 浏览 1 评论 0原文

今天早上我真的需要去别的地方。因此,我决定在这里发布一个性能问题。

下面的代码有效,但它多次调用 Load 和 Save 方法。这似乎远没有效率。请有人提供到目前为止加载和保存行发生在循环之外的代码。我只想调用一次加载和保存。

谢谢各位:)

 public void RemoveNodes(IList<String> removeItems)

    {

        foreach (String removeItem in removeItems)

        {

            XDocument document = XDocument.Load(fullFilePath);

            var results = from item in document.Descendants(elementName)

                          let attr = item.Attribute(attributeName)

                          where attr != null && attr.Value == removeItem.ToString()

                          select item;

            results.ToList().ForEach(item => item.Remove());

            document.Save(fullFilePath);

        }

    }

I really need to be somewhere else this morning. So, I have decided to post a performance question here instead.

The code below works but it calls Load and Save method multiple times. This seems far from efficient. Please could someone provide the code so far the load and save lines occur outside the loop. I wish to call load and save only once.

Thanks chaps :)

 public void RemoveNodes(IList<String> removeItems)

    {

        foreach (String removeItem in removeItems)

        {

            XDocument document = XDocument.Load(fullFilePath);

            var results = from item in document.Descendants(elementName)

                          let attr = item.Attribute(attributeName)

                          where attr != null && attr.Value == removeItem.ToString()

                          select item;

            results.ToList().ForEach(item => item.Remove());

            document.Save(fullFilePath);

        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雪碎 2024-10-31 06:39:58

您自己已经给出了答案 - 只需将 LoadSave 调用移到循环之外即可。我不清楚您自己在实现这一点时遇到了问题...

不过您也可以使您的查询稍微简单一些:

XDocument document = XDocument.Load(fullFilePath);
foreach (String removeItem in removeItems)
{
    var results = from item in document.Descendants(elementName)
                  where (string) item.Attribute(attributeName) == removeItem
                  select item;
    results.ToList().ForEach(item => item.Remove());
}
document.Save(fullFilePath);

这使用了从 XAttributestring 的转换这一事实> 如果属性引用本身为 null,则返回 null。

您甚至不需要使用查询表达式:

var results = document.Descendants(elementName)
          .Where(item => (string) item.Attribute(attributeName) == removeItem);

You've already given the answer yourself - just move the Load and Save calls outside the loop. It's not clear to me where you were having problems implementing that yourself...

You can make your query slightly simpler too though:

XDocument document = XDocument.Load(fullFilePath);
foreach (String removeItem in removeItems)
{
    var results = from item in document.Descendants(elementName)
                  where (string) item.Attribute(attributeName) == removeItem
                  select item;
    results.ToList().ForEach(item => item.Remove());
}
document.Save(fullFilePath);

This uses the fact that the conversion from XAttribute to string returns null if the attribute reference itself is null.

You don't even need to use a query expression:

var results = document.Descendants(elementName)
          .Where(item => (string) item.Attribute(attributeName) == removeItem);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文