在XDocument中定位和设置元素值最高效的方法是什么?给定以下XML“模板”:{USERNAME}{PASSWORD}{CLIENT-ID}{SERVICE-ID}使用XDocument,在花括号中设置值的最佳方式是什么。到目前为止,我一直坚持选择元素中三个节点中每一个节点的最佳方法。这只是XML的一小部分,其他的可能包含多达20个元素。XML的结构方式不是我的创造,而是我们必须发送给供应商的“Web服务”……在有人嘲笑format="count"属性之前:)@David-为响应干杯,赞赏。我有点希望它更优雅一点,比如:Liste=doc.Descendants("a").ToList();e.Where(x=>x.Attributes("name")=="username").Single().Value="abc";e.Where(x=>x.Attributes("name")=="password").Single().Value="abc";显然,上面的代码不起作用,但我假设每个标签都有一个优雅的衬里,这对你有用吗?好旧的后代财产。字符串xmlInput=...;XDocumentmyDoc=XDocument.Parse(xmlInput);//列表someElements=myDoc.Descendants("a").ToList();someElements.ForEach(x=>x.Value="Foo");//控制台.WriteLine(myDoc);好吧,我看到你有财产。也这样做:stringxmlInput=//...XDocumentmyDoc=XDocument.Parse(xmlInput);//ListsomeText=myDoc.Descendants().Nodes().OfType().Where(x=>x.Value.StartsWith("{")&&x.Value.EndsWith("}")).ToList();//列表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");//控制台.WriteLine(myDoc);啊,现在我有了你所期待的,我会让它工作:以上是C#学习教程:在XDocument中定位和设置元素值的最有效方法是什么?如果分享的内容对你有用,需要进一步了解C#学习教程,希望你多多关注——Liste=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";本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
