什么是定位在一个XDocument设置元素值的最有效方法是什么? [英] What's the most efficient way to locate and set element values in an XDocument?

查看:158
本文介绍了什么是定位在一个XDocument设置元素值的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下XML模板:

 <请求模块=CRM呼叫=list_service_featuresID ={ID}>
  <块名=身份验证>
     &其中;一个名字=username的格式=文本> {USERNAME}&所述; / a取代;
     < A NAME =密码的格式=密码> {PASSWORD}< / A>
     <名称=客户端ID格式=计数> {CLIENT-ID}< / A>
  < /块GT;
  &其中;一个名字=服务ID格式=计数> {SERVICE-ID}&所述; / a取代;
< /请求>
 

使用的XDocument,什么是在大括号设置的值的最佳方式。我有这么远,但就死在最好的方式来选择这三个<有/>块/>在&LT内部节点; 元素。这仅仅是XML的一个小片段,其他人可能有多达20 < A NAME =...>< / A> 元素。

表示XML的构造方式不是我的创​​造,这是我们必须发送到我们​​的供应商的'网络服务'...之前,任何人都有嘲笑格式=计数的属性:)

<一个href="http://stackoverflow.com/questions/299747/whats-the-most-efficient-way-to-locate-and-set-element-values-in-an-xdocument#299887">@David - 欢呼声回应,AP preciated。我有点希望它会是更优雅一点,还挺线沿线的:

 名单,其中,的XElement&GT; E = doc.Descendants(A)了ToList()。
e.Where。(X =&GT; x.Attributes(名)==用户名)单()值=ABC;
e.Where。(X =&GT; x.Attributes(名)==密码)单()值=ABC;
 

显然,code以上不工作,但我认为会有一个优雅的单行为每个&LT; A&GT; 标签

解决方案

这是否为你做它?好老后人属性。

 字符串xmlInput = ...;
的XDocument myDoc = XDocument.Parse(xmlInput);
//
名单&LT;的XElement&GT; someElements = myDoc.Descendants(一)了ToList()。
someElements.ForEach(X =&GT; x.Value =富);
//
Console.WriteLine(myDoc);
 

嗯,我看到你有一个属性在里面。也可以这样做:

 字符串xmlInput = // ...
的XDocument myDoc = XDocument.Parse(xmlInput);
//
名单&LT; XTEXT&GT; someText =
  myDoc.Descendants()
  .Nodes()
  .OfType&LT; XTEXT&GT;()
  。凡(X =&GT; x.Value.StartsWith({)及&安培; x.Value.EndsWith(}))
  .ToList();
//
名单&LT; XAttribute&GT; someAttributes =
  myDoc.Descendants()
  .Attributes()
  。凡(X =&GT; x.Value.StartsWith({)及&安培; x.Value.EndsWith(}))
  .ToList();
//
someText.ForEach(X =&GT; x.Value =富);
someAttributes.ForEach(X =&GT; x.Value =酒吧);
//
Console.WriteLine(myDoc);
 

嗯,现在你期待什么,我会使其工作:

 名单,其中,的XElement&GT; E = myDoc.Descendants(A)了ToList()。
e.Where(X =&GT; x.Attribute(name)的价值==用户名)单()值=ABC。
e.Where(X =&GT; x.Attribute(name)的价值==密码)单()值=ABC。
 

Given the following XML 'template':

<Request module="CRM" call="list_service_features" id="{ID}">
  <block name="auth">
     <a name="username" format="text">{USERNAME}</a>
     <a name="password" format="password">{PASSWORD}</a>
     <a name="client-id" format="counting">{CLIENT-ID}</a>
  </block>
  <a name="service-id" format="counting">{SERVICE-ID}</a>
</Request>

Using XDocument, what's the best way to set the values in curly braces. I got so far but got stuck on the best way to select each of the three <a /> nodes inside the <block/> element. This is just a small fragment of XML, others may have up to 20 <a name="..."></a> elements.

The the way the XML is constructed wasn't my creation, it's what we have to sent to our supplier's 'web service'...before anyone has a laugh at the format="counting" attribute :)

@David - cheers for the response, appreciated. I was kinda hoping it'd be a bit more elegant, kinda along the lines of:

List<XElement> e = doc.Descendants("a").ToList();
e.Where(x => x.Attributes("name") == "username").Single().Value = "abc";
e.Where(x => x.Attributes("name") == "password").Single().Value = "abc";

Clearly the code above doesn't work but I thought there would be an elegant one liner for each of the <a> tags

解决方案

Does this do it for you? Good old Descendants property.

string xmlInput = ...;
XDocument myDoc = XDocument.Parse(xmlInput);
//
List<XElement> someElements = myDoc.Descendants("a").ToList();
someElements.ForEach(x => x.Value = "Foo");
//
Console.WriteLine(myDoc);

Hmm, I see you have an attribute in there. Can do that too:

string xmlInput = //...
XDocument myDoc = XDocument.Parse(xmlInput);
//
List<XText> someText =
  myDoc.Descendants()
  .Nodes()
  .OfType<XText>()
  .Where(x => x.Value.StartsWith("{") && x.Value.EndsWith("}"))
  .ToList();
//
List<XAttribute> someAttributes =
  myDoc.Descendants()
  .Attributes()
  .Where(x => x.Value.StartsWith("{") && x.Value.EndsWith("}"))
  .ToList();
//
someText.ForEach(x => x.Value = "Foo");
someAttributes.ForEach(x => x.Value = "Bar");
//
Console.WriteLine(myDoc);

Ah, now with what you're expecting, I will make it work:

List<XElement> e = myDoc.Descendants("a").ToList();
e.Where(x => x.Attribute("name").Value == "username").Single().Value = "abc";
e.Where(x => x.Attribute("name").Value == "password").Single().Value = "abc";

这篇关于什么是定位在一个XDocument设置元素值的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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