使用包含连字符/破折号的属性名称搜索XElement [英] Searching for XElement with attribute name that contain hyphens/dashes

查看:164
本文介绍了使用包含连字符/破折号的属性名称搜索XElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我在VB.Net中编写了一些代码,使用XElement,XDocument等来存储和操作HTML。某些HTML使用包含连字符/短划线( - )的属性名称。我遇到了使用LinqToXml通过这些属性搜索XElements的问题。



当时我发现了一篇文章(现在找不到它),它指出了VB.net中的解决方案使用这样的语法:
$ b $ pre $ Dim rootElement as XElement = GetARootXElement()
Dim query = From p在rootElement < DIV>其中p。@< data-qid> = 5选择p

魔术语法是@<>,它以某种方式翻译连字符的属性名称转换成Linq可以成功使用的格式。这段代码在VB.Net中效果很好。



现在的问题是,我们现在已经将所有VB.Net代码转换为C#,并且转换实用程序在此语法上窒息。在VB.Net中我找不到任何关于这个魔术语法的东西,所以我希望有人能够填写我的细节,特别是C#的等价物。感谢。



以下是一个例子:

 < div id = '东西' > 
< div id ='stuff2'>
< div id ='stuff'data-qid = 5>
<! - more html - >
< / div>
< / div>
< / div>

在上面的代码中,rootElement是div的东西,我想要搜索内部div与attribuate data-qid = 5。

解决方案

我可以在C#中编译以下代码 - 我认为它相当于原始的VB(请注意原始的VB有Option Strict Off):

  XElement rootElement = GetARootXElement(); 
var query = from rootElement.Elements(div)
其中p.Attribute(data-qid)。Value == 5.ToString()
select p;

这是我的(修改过的)测试,它用'data-qid'属性找到div: / p>

  var xml = System.Xml.Linq.XElement.Parse(< div id ='stuff'>< div id ='stuff2'>< div id ='stuff3'data-qid ='5'><! -  more html  - >< / div>< / div>< / div> ); 
var rootElement = xml.Element(div);
var query = from rootElement.Elements(div)
其中p.Attribute(data-qid)。Value == 5.ToString()
select p;


I wrote some code in VB.Net a while ago that is using XElement, XDocument, etc... to store and manipulate HTML. Some of the HTML makes use of attribute names that contain a hyphen/dash (-). I encountered issues using LinqToXml to search for XElements by these attributes.

Back then I found an article (can't find it now) that indicated the solution in VB.net was to use syntax like this:

Dim rootElement as XElement = GetARootXElement()
Dim query = From p In rootElement.<div> Where p.@<data-qid> = 5 Select p

The "magic" syntax is the @<> which somehow translates the hyphenated attribute name into a format that can be successfully used by Linq. This code works great in VB.Net.

The problem is that we have now converted all the VB.Net code to C# and the conversion utility choked on this syntax. I can't find anything about this "magic" syntax in VB.Net and so I was hoping someone could fill in the details for me, specifically, what the C# equivalent is. Thanks.

Here is an example:

<div id='stuff'>
    <div id='stuff2'>
        <div id='stuff' data-qid=5>
            <!-- more html -->
        </div>
    </div>
</div>

In my code above the rootElement would be the stuff div and I would want to search for the inner div with the attribuate data-qid=5.

解决方案

I can get the following to compile in C# - I think it's equivalent to the original VB (note that the original VB had Option Strict Off):

XElement rootElement = GetARootXElement();
var query = from p in rootElement.Elements("div")
            where p.Attribute("data-qid").Value == 5.ToString()
            select p;

Here's my (revised) test, which finds the div with the 'data-qid' attribute:

var xml = System.Xml.Linq.XElement.Parse("<div id='stuff'><div id='stuff2'><div id='stuff3' data-qid='5'><!-- more html --></div></div></div>");
var rootElement = xml.Element("div");
var query = from p in rootElement.Elements("div")
    where p.Attribute("data-qid").Value == 5.ToString()
    select p;

这篇关于使用包含连字符/破折号的属性名称搜索XElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆