如何使用C#替换xml中的节点 [英] How to replace node in an xml using c#

查看:108
本文介绍了如何使用C#替换xml中的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要使用c#将下面的xml中的"DETAILS"节点替换为"ITEMDETAILS".
我尝试使用ReplaceChild方法,但无法做到这一点.

Hi,

I need to replace the ''DETAILS'' node with ''ITEMDETAILS'' in the below xml using c#.
I have tried with ReplaceChild method, but could not do that.

<?xml version="1.0" ?>
<Root>
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<H1>A</H1>
<H2>B</H2>
<H3>C</H3>
<H4>D</H4>
</Header>
<DETAILS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Item>
<Item1>I1</Item1>
<Item2>I2</Item2>
</Item>
<Item>
<Item1>I3</Item1>
<Item2>I4</Item2>
</Item>
</DETAILS>
</Root>



有人可以帮我吗?

谢谢



can someone please help me in this?

Thanks

推荐答案

如果要使用XmlDocument,则:

If you want to use XmlDocument, then:

var doc = new XmlDocument();
doc.LoadXml(" ... some xml ... ");
var root = doc.GetElementsByTagName("Root")[0];
var oldElem = root.SelectSingleNode("DETAILS");
var newElem = doc.CreateElement("ITEMDETAILS");
root.ReplaceChild(newElem, oldElem);
while (oldElem.ChildNodes.Count != 0)
{
    newElem.AppendChild(oldElem.ChildNodes[0]);
}
while (oldElem.Attributes.Count != 0)
{
    newElem.Attributes.Append(oldElem.Attributes[0]);
}


XElement xe = XElement.Load("XML FILE PATH");
            XElement oldnode = xe.Element("DETAILS");
            XElement newnode = new XElement("ITEMDETAILS", oldnode.Elements());
            foreach (var item in oldnode.Attributes())
            {
                newnode.SetAttributeValue(item.Name, item.Value);
            }
            xe.Add(newnode);
            oldnode.Remove();


ReplaceChild可以正常工作.如果没有您的代码(或对出现的错误的解释),很难知道您出了什么问题,但请检查以下内容:

1.您具有正确的参数,它是ReplaceChild(new, old)

2.必须从同一文档创建新节点

3.替换子代必须在旧节点的祖代上完成

一件事是,如果要将DETAILS重命名为ITEMDETAILS,则需要将所有属性和子节点复制到新节点上,否则将获得以下信息:

ReplaceChild works fine. Without your code (or an explanation of what error you are getting) it is hard to know what is going wrong for you but check the following:

1. You have the parameters the right way round, it is ReplaceChild(new, old)

2. The new node must be created from the same document

3. The replace child must be done on an ancestor of the old node

One thing is that if you want to rename DETAILS to ITEMDETAILS then you need to copy all the attributes and child nodes across to the new node, otherwise you will get the following:

<?xml version="1.0"?>
<Root>
  <Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <H1>A</H1>
    <H2>B</H2>
    <H3>C</H3>
    <H4>D</H4>
  </Header>
  <ITEMDETAILS />
</Root&gt;



我怀疑不是您想要的.您需要做的是将每个子元素和属性复制到新节点,如



Which I suspect is not what you want. What you need to do is copy each child element and attribute to the new node, as shown here[^]


这篇关于如何使用C#替换xml中的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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