什么是定位在一个XDocument设置元素值的最有效方法是什么?最有效、元素、方法、XDocument

2023-09-03 07:37:28 作者:我知道坚强对于我的意义x

由于以下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的构造方式不是我的创​​造,这是我们必须发送到我们​​的供应商的'网络服务'...之前,任何人都有嘲笑格式=计数的属性:)

@David - 欢呼声回应,AP preciated。我有点希望它会是更优雅一点,还挺线沿线的:

 名单,其中,的XElement> E = doc.Descendants(A)了ToList()。
e.Where。(X => x.Attributes(名)==用户名)单()值=ABC;
e.Where。(X => x.Attributes(名)==密码)单()值=ABC;
 
y x 1 x的反函数

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

解决方案

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

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

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

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

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

 名单,其中,的XElement> E = myDoc.Descendants(A)了ToList()。
e.Where(X => x.Attribute(name)的价值==用户名)单()值=ABC。
e.Where(X => 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";

 
精彩推荐
图片推荐