如何更改具有相同名称的多个子节点的值 [英] How to change the value of multiple child nodes with same name
问题描述
大家好,
我有一个要求,我必须将每个具有相同名称的子节点的值更新为null()。数据类型为XElement(XML)。下面是我到目前为止尝试的代码段。
if (xmlData.Elements( attachment)。Any()){
foreach (XElement节点 in xmlData.Elements( attachment) ))
{
if (nodes.Value!= ){
xmlData.SetElementValue(nodes.Name, 跨度>);
}
}
}
if (xmlData.Elements( comment)。Any()&& xmlData.Elements( comment)。元素( comment_attachment) .Any()){
foreach (XElement节点 in xmlData.Elements( comment)。元素( comment_attachment)){
if (nodes.Value!= ){
xmlData.SetElementValue(nodes.Name, );
}
}
}
这里xmlData是包含巨大XML父级的XElement对象节点。在这个节点内,我必须清除节点 attachment 和 comment_attachment 的内容,这些节点本质上是编码为二进制数据并以XML格式存储的文件。
我试图使用SetElementValue方法,该方法仅将第一个子值设置为null,但不是所有子项(attachment或comment_attachment节点)。
还有其他替代品吗?
提前致谢。
试试RemoveNodes
方法 [ ^ ]:
foreach (XElement node in xmlData.Elements( attachment))
{
node.RemoveNodes();
}
foreach (XElement节点 in xmlData.Elements( comment)。元素( comment_attachment))
{
node.RemoveNodes();
}
如果您想清除属性,请使用RemoveAll
方法 [ ^ ]。
注意:此代码只能找到< attachment>
和< comment>
元素,它们是xmlData
节点的直接子元素。
关于OP的评论...
看看例子:
< span class =code-comment> // create sample xml(input)
string xcontent = @ < Boards>
< Board class ='support'appid = '1'>
< Notes>
< Note class ='message'id ='1'source ='client'>早上好!当我尝试将大部分文本复制到剪贴板时,我的应用程序挂起。< / Note>
< Note class ='message'id ='2'source ='stuff'>早上好!< / Note>
< Note class ='question'id ='3'source ='stuff'>您是否尝试复制较少部分的文字?< / Note>
< Note class ='message'id ='4'source ='client'>等一下。我会试试。< / Note>
< Note class ='message'id ='5'source ='client'>抱歉,仍有相同的错误。< / Note>
< Note class ='message'id ='6'source ='stuff'>谢谢!好像,这是一个错误。我会向开发者报告。< / Note>
< Note class ='question'id ='7'source ='stuff'>您还有其他问题吗?< / Note>
< Note class ='message'id ='8'source ='client'>不。谢谢。< / Note>
< Note class ='message'id ='9'source ='stuff'>谢谢。很快,您将获得有关更新的信息。< / Note>
< / Notes>
< / Board>
< / Boards>;
XDocument xdoc = XDocument.Parse(xcontent);
// 获取具有特定属性的节点;
var nodesToClean = xdoc.Descendants( 注意)。其中(x => x.Attribute( class)。值== 问题);
// 用空字符串替换值
foreach (XElement xele in nodesToClean)
{
xele.Value = string .Empty;
}
// 文档已更改!!!
结果:
< Boards >
< Board class = support appid = 1 >
< 注释 >
< 注意 class = 消息 id = 1 source = client > 早上好!当我尝试将大部分文本复制到剪贴板时,我的应用程序挂起。< / Note >
< 注意 < span class =code-attribute> class = message id = 2 source = stuff > 早上好!< / Note >
< 注意 class = 问题 id = 3 source = stuff > < /注意 > ;
< 注意 class = message id =4 source = client > 等一下。我会试试。< / Note >
< 注意 class = message < span class =code-attribute> id = 5 source = 客户端 > 抱歉,仍然是同样的错误。< / Note >
< 注意 class = message id = 6 source = stuff > 谢谢!好像,这是一个错误。我将向开发人员报告。< / Note >
< 注意 class = 问题 id = 7 source = stuff > < / Note >
< 注意class = message id = 8 source = client > 否。谢谢。< /注意 >
< 注意 class = message id = 9 source = stuff > 谢谢。很快,您将获得有关更新的信息。< / Note >
< / Notes >
< / Board >
< / Boards > 跨度>
Hi All,
I have a requirement wherein I have to update the values of each child nodes with the same name to null (""). The data is of type XElement (XML). Below is the snippet i tried so far.
if (xmlData.Elements("attachment").Any()) {
foreach (XElement nodes in xmlData.Elements("attachment"))
{
if (nodes.Value != "") {
xmlData.SetElementValue(nodes.Name, "");
}
}
}
if (xmlData.Elements("comment").Any() && xmlData.Elements("comment").Elements("comment_attachment").Any()) {
foreach (XElement nodes in xmlData.Elements("comment").Elements("comment_attachment")) {
if (nodes.Value != "") {
xmlData.SetElementValue(nodes.Name, "");
}
}
}
Here the xmlData is the XElement object containing a huge XML parent node. Inside this node I have to clear the contents of the nodes attachment and comment_attachment, which are essentially files encoded as binary data and stored in XML.
I tried to use the SetElementValue method which sets only the first child value to null, but not for all the children (attachment or comment_attachment nodes).
Are there any other alternatives for this?
Thanks in advance.
Try theRemoveNodes
method[^]:
foreach (XElement node in xmlData.Elements("attachment")) { node.RemoveNodes(); } foreach (XElement node in xmlData.Elements("comment").Elements("comment_attachment")) { node.RemoveNodes(); }
If you want to clear the attributes as well, use theRemoveAll
method[^].
NB: This code will only find<attachment>
and<comment>
elements which are direct children of thexmlData
node.
As to the OP's comments...
Have a look at example:
//create sample xml (input) string xcontent = @"<Boards> <Board class='support' appid='1'> <Notes> <Note class='message' id='1' source='client'>Good morning! My application hangs when i try to copy large portion of text to the clipboard.</Note> <Note class='message' id='2' source='stuff'>Good morning!</Note> <Note class='question' id='3' source='stuff'>Have you tried to copy less portion of text?</Note> <Note class='message' id='4' source='client'>Wait a moment. I'll try.</Note> <Note class='message' id='5' source='client'>Sorry, still the same error.</Note> <Note class='message' id='6' source='stuff'>Thank you! Seems, it's a bug. I'll report it to the developer.</Note> <Note class='question' id='7' source='stuff'>Do you have another question?</Note> <Note class='message' id='8' source='client'>No. Thank you.</Note> <Note class='message' id='9' source='stuff'>Thank you. Soon you'll get information about an update.</Note> </Notes> </Board> </Boards>"; XDocument xdoc = XDocument.Parse(xcontent); //get nodes with specific attribute; var nodesToClean = xdoc.Descendants("Note").Where(x=>x.Attribute("class").Value=="question"); //replace value with empty string foreach (XElement xele in nodesToClean) { xele.Value = string.Empty; } //document has been changed!!!
Result:
<Boards> <Board class="support" appid="1"> <Notes> <Note class="message" id="1" source="client">Good morning! My application hangs when i try to copy large portion of text to the clipboard.</Note> <Note class="message" id="2" source="stuff">Good morning!</Note> <Note class="question" id="3" source="stuff"></Note> <Note class="message" id="4" source="client">Wait a moment. I'll try.</Note> <Note class="message" id="5" source="client">Sorry, still the same error.</Note> <Note class="message" id="6" source="stuff">Thank you! Seems, it's a bug. I'll report it to the developer.</Note> <Note class="question" id="7" source="stuff"></Note> <Note class="message" id="8" source="client">No. Thank you.</Note> <Note class="message" id="9" source="stuff">Thank you. Soon you'll get information about an update.</Note> </Notes> </Board> </Boards>
这篇关于如何更改具有相同名称的多个子节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!