使用 linq 仅替换 Xelement 中的值
编辑:更详细的 HTML 文档...简而言之-我实际上如何进行查找以及 element.setvalue 或 element.value 应该出现在查询中的确切位置...
编辑 2:猴子 id 列表看起来不清晰,因此我将添加正确的 id 并向我的查找数据对象添加其他属性,对于造成混乱表示歉意!我使用列表的原因是因为我的数据源可能来自任何地方,我也使用了列表对象,因为我真的不知道字典的正确用法(我是编码新手,因此为什么我的问题到处都是,请耐心等待)
我有一个 XElement,它是一个格式正确的 HTML 文档,我试图仅用列表对象中包含的值替换 html 元素的值,例如
<div id="pageContainer">
<p> some guy wants to <b>buy</b> a <h4><label id="monkey23">monkeyfield</label></h4> for some price that I do not have a clue about, maybe we should <i>suggest</i> a list of other monkeys he may like:
</p>
<h3>list of special monekeys you may want chappy...</h3>
<br />
<ul>
<li><label id="monkey13">monkeyfield</label></li>
<li><label id="monkey3">monkeyfield</label></li>
<li><label id="animal4">animalfield</label></li>
<li><label id="seacreature5">seacreaturefield</label></li>
<li><label id="mamal1">mamal field</label></li>
</ul>
</div>
注意:值“monkeyfield”是临时值在屏幕上插入的目的是为了识别这是一个字段,一旦绑定了数据源中的值,就会出现新值。
public class LookupData
{
public string id{get;set;}
public string value{get;set;}
public string Type{get;set;}
public string Url{get;set;}
}
...
public void DataTransformerMethod()
{
var data = new List<LookupData>();
data.add(new LookupData{id="monkey3", value="special monkey from africa" });
data.add(new LookupData{id="monkey13", value="old monkey from china" });
data.add(new LookupData{id="seacreature5", value="sea monkey" });
data.add(new LookupData{id="animal4", value="rhino" });
data.add(new LookupData{id="mamal1", value="some mamal creature" });
//what linq query will iterate over the document and set the values from the values
//found in the list?
var answer = from x in HtmlDocAsAXelement
where x.Attributes()
.Any(a=> data.AsEnumerable().Where(f=> f.Name == a.Name) );
//somehow I should use .SetValue(a.value)???
SaveTheNewXElement(answer ); //all other original data must stay in tact...
}
EDIT: A bit more detailed HTML document... In short- how do I actually do the lookup and where precisely should the element.setvalue or element.value appear in the query...
Edit 2: The list of monkey id does not appear clear so I will add proper id's and add additional properties to my Lookup data object, sorry for the confusion! The reason I have used a list is bacause my datasource could be from anywhere also I have used a List object because I do not really know the proper usage of Dictionary (I am a newbie to coding hence why my question is all over the place, please bear with me)
I have an XElement which is a properly formatted HTML document, I am trying to replace only the value of a html element with a value contained in a List Object for example
<div id="pageContainer">
<p> some guy wants to <b>buy</b> a <h4><label id="monkey23">monkeyfield</label></h4> for some price that I do not have a clue about, maybe we should <i>suggest</i> a list of other monkeys he may like:
</p>
<h3>list of special monekeys you may want chappy...</h3>
<br />
<ul>
<li><label id="monkey13">monkeyfield</label></li>
<li><label id="monkey3">monkeyfield</label></li>
<li><label id="animal4">animalfield</label></li>
<li><label id="seacreature5">seacreaturefield</label></li>
<li><label id="mamal1">mamal field</label></li>
</ul>
</div>
Note: the value "monkeyfield" is a temporary value inserted onscreen for the purpose of identifying this is a field, once the values from the data source is binded the new values should appear.
public class LookupData
{
public string id{get;set;}
public string value{get;set;}
public string Type{get;set;}
public string Url{get;set;}
}
...
public void DataTransformerMethod()
{
var data = new List<LookupData>();
data.add(new LookupData{id="monkey3", value="special monkey from africa" });
data.add(new LookupData{id="monkey13", value="old monkey from china" });
data.add(new LookupData{id="seacreature5", value="sea monkey" });
data.add(new LookupData{id="animal4", value="rhino" });
data.add(new LookupData{id="mamal1", value="some mamal creature" });
//what linq query will iterate over the document and set the values from the values
//found in the list?
var answer = from x in HtmlDocAsAXelement
where x.Attributes()
.Any(a=> data.AsEnumerable().Where(f=> f.Name == a.Name) );
//somehow I should use .SetValue(a.value)???
SaveTheNewXElement(answer ); //all other original data must stay in tact...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您需要迭代所有需要更改的 XElement,并通过调用
Value
设置器来设置它们的值:element.Value = "newvalue";
如果元素有多个文本节点并且您只想更改其中之一,那么情况会更棘手,但由于元素内没有其他内容,这对您来说应该没问题。
编辑:讨论后,我会做这样的事情:
Well, you need to iterate over all the XElements which need changing - and set their value by just calling the
Value
setter:element.Value = "newvalue";
It would be trickier if the element had multiple text nodes and you only wanted to change one of them, but as there's no other content within the element, this should be fine for you.
EDIT: After the discussion, I would do something like this:
您无法“轻松”做到这一点,因为 LINQ 中没有 。
foreach
等效项。这是故意的。请参阅 http://blogs.msdn .com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx 和 LINQ 等效项foreach 用于 IEnumerable我建议您对查询结果执行正常的
foreach
操作。You can't do that "easily", because there is no
foreach
equivalent in LINQ. That's on purpose. See http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx and LINQ equivalent of foreach for IEnumerable<T>.I would suggest you just do a normal
foreach
over the query results.