change XmlElement Name property

我怕爱的太早我们不能终老 提交于 2019-12-11 07:43:21

问题


I would like to change the Name property of an XmlElement in c++/cli.

I would like to do myXmlElem.Name = "xyz", but the compiler tells me that I can't do a set operation on the Name property.

i.e.

<abc/>

changed to

<xyz/>

How can I achieve this?

Thanks!


回答1:


you can't change the Name property of an XmlElement like that (Name is read only).

you can however do something like the following (example in C#).

XmlElement xyz = myXmlElem.OwnerDocument.CreateElement("xyz");
myXmlElem.ParentNode.ReplaceChild(xyz, myXmlElem);

EDIT In response to your comment

XmlElement xyz = myXmlElem.OwnerDocument.CreateElement("xyz");

for(int i = 0; i < myXmlElem.ChildNodes.Count; i++){
    XmlNode child = myXmlElem.ChildNodes[i];
    xyz.AppendChild(child.CloneNode(true));
}

myXmlElem.ParentNode.ReplaceChild(xyz, myXmlElem);



回答2:


You can use Linq to Xml which supports changing the name of an XElement:

XDocument doc = XDocument.Parse("<foo><bar /></foo>");
doc.Root.Name = "changed";//now it will look like <changed><bar /></changed>


来源:https://stackoverflow.com/questions/12320809/change-xmlelement-name-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!