使用 linq 仅替换 Xelement 中的值

发布于 2024-10-20 23:13:34 字数 2253 浏览 3 评论 0原文

编辑:更详细的 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 技术交流群。

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

发布评论

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

评论(2

傲娇萝莉攻 2024-10-27 23:13:34

好吧,您需要迭代所有需要更改的 XElement,并通过调用 Value 设置器来设置它们的值:

element.Value = "newvalue";

如果元素有多个文本节点并且您只想更改其中之一,那么情况会更棘手,但由于元素内没有其他内容,这对您来说应该没问题。

编辑:讨论后,我会做这样的事情:

Dictionary<string, string> replacements = data.ToDictionary(x => x.id,
                                                            x => x.value);

foreach (XElement element in HtmlDocAsAXelement.Descendants())
{
    string newValue;
    string id = (string) element.Attribute("id");
    if (id != null && replacements.TryGetValue(id, out newValue))
    {
        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:

Dictionary<string, string> replacements = data.ToDictionary(x => x.id,
                                                            x => x.value);

foreach (XElement element in HtmlDocAsAXelement.Descendants())
{
    string newValue;
    string id = (string) element.Attribute("id");
    if (id != null && replacements.TryGetValue(id, out newValue))
    {
        element.Value = newValue;
    }
}
如果没有 2024-10-27 23:13:34

您无法“轻松”做到这一点,因为 LINQ 中没有 foreach 等效项。这是故意的。请参阅 http://blogs.msdn .com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspxLINQ 等效项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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文