更有效地过滤 XDocument

发布于 2024-10-05 21:42:34 字数 1605 浏览 6 评论 0原文

我想从 XML 文档中过滤高性能 XML 元素。

以这个包含联系人的 XML 文件为例:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="asistentes.xslt"?>
<contactlist evento="Cena Navidad 2010" empresa="company">
  <contact type="1" id="1">
    <name>Name1</name>
    <email>[email protected]</email>
    <confirmado>SI</confirmado>
  </contact>
  <contact type="1" id="2">
    <name>Name2</name>
    <email>[email protected]</email>
    <confirmado>Sin confirmar</confirmado>
  </contact>
</contaclist>

我当前要从此 XML 文档进行过滤的代码:

using System; 
using System.Xml.Linq; 

class Test 
{ 
   static void Main() 
   { 
      string xml = @" the xml above"; 
      XDocument doc = XDocument.Parse(xml); 

      foreach (XElement element in doc.Descendants("contact")) {
         Console.WriteLine(element);
         var id = element.Attribute("id").Value;
         var valor = element.Descendants("confirmado").ToList()[0].Value;
         var email = element.Descendants("email").ToList()[0].Value;
         var name = element.Descendants("name").ToList()[0].Value;
         if (valor.ToString() == "SI") { }
      }
   } 
} 

优化此代码以过滤 元素内容的最佳方法是什么?

I would like to filter with high performance XML elements from an XML document.

Take for instance this XML file with contacts:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="asistentes.xslt"?>
<contactlist evento="Cena Navidad 2010" empresa="company">
  <contact type="1" id="1">
    <name>Name1</name>
    <email>[email protected]</email>
    <confirmado>SI</confirmado>
  </contact>
  <contact type="1" id="2">
    <name>Name2</name>
    <email>[email protected]</email>
    <confirmado>Sin confirmar</confirmado>
  </contact>
</contaclist>

My current code to filter from this XML document:

using System; 
using System.Xml.Linq; 

class Test 
{ 
   static void Main() 
   { 
      string xml = @" the xml above"; 
      XDocument doc = XDocument.Parse(xml); 

      foreach (XElement element in doc.Descendants("contact")) {
         Console.WriteLine(element);
         var id = element.Attribute("id").Value;
         var valor = element.Descendants("confirmado").ToList()[0].Value;
         var email = element.Descendants("email").ToList()[0].Value;
         var name = element.Descendants("name").ToList()[0].Value;
         if (valor.ToString() == "SI") { }
      }
   } 
} 

What would be the best way to optimize this code to filter on <confirmado> element content?

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

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

发布评论

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

评论(2

甲如呢乙后呢 2024-10-12 21:42:34
var doc = XDocument.Parse(xml); 

var query = from contact in doc.Root.Elements("contact")
            let confirmado = (string)contact.Element("confirmado")
            where confirmado == "SI"
            select new
            {
                Id    = (int)contact.Attribute("id"),
                Name  = (string)contact.Element("name"),
                Email = (string)contact.Element("email"),
                Valor = confirmado
            };

foreach (var contact in query)
{
    ...
}

兴趣点:

  • doc.Root.Elements("contact") 仅选择文档根中的 元素,而不是搜索整个文档对于 元素。

  • XElement.Element 方法 返回具有给定名称的第一个子元素。无需将子元素转换为列表并获取第一个元素。

  • XElementXAttribute 类提供了多种

var doc = XDocument.Parse(xml); 

var query = from contact in doc.Root.Elements("contact")
            let confirmado = (string)contact.Element("confirmado")
            where confirmado == "SI"
            select new
            {
                Id    = (int)contact.Attribute("id"),
                Name  = (string)contact.Element("name"),
                Email = (string)contact.Element("email"),
                Valor = confirmado
            };

foreach (var contact in query)
{
    ...
}

Points of interest:

  • doc.Root.Elements("contact") selects only the <contact> elements in the document root, instead of searching the whole document for <contact> elements.

  • The XElement.Element method returns the first child element with the given name. No need to convert the child elements to a list and take the first element.

  • The XElement and XAttribute classes provide a wide selection of convenient conversion operators.

别理我 2024-10-12 21:42:34

您可以使用 LINQ:

foreach (XElement element in doc.Descendants("contact").Where(c => c.Element("confirmado").Value == "SI"))

You could use LINQ:

foreach (XElement element in doc.Descendants("contact").Where(c => c.Element("confirmado").Value == "SI"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文