473,769 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Renaming an element

Say that I have an element <elementA> that has several layers of subelements.
In System.Xml.XmlD ocument and related classes, how do I rename <elementA> to <elementB> leaving the subelements intact?
Nov 12 '05 #1
1 14611


pjeung wrote:
Say that I have an element <elementA> that has several layers of subelements.
In System.Xml.XmlD ocument and related classes, how do I rename <elementA> to <elementB> leaving the subelements intact?


I think you have to write your own code that creates a new element with
the desired name, then you need to move the attribute and child nodes to
the new element and finally you need to replace the old element with the
new element. Here is a C# example that does that for element nodes:

using System;
using System.Xml;

public class Test20040721 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(@"test2004072 101.xml");
Console.WriteLi ne(xmlDocument. OuterXml);
XmlElement element = (XmlElement)
xmlDocument.Get ElementsByTagNa me("element")[0];
XmlElement renamedElement = (XmlElement) RenameNode(elem ent, null,
"new-element");
Console.WriteLi ne(xmlDocument. OuterXml);
}

public static XmlNode RenameNode (XmlNode node, string namespaceURI,
string qualifiedName) {
if (node.NodeType == XmlNodeType.Ele ment) {
XmlElement oldElement = (XmlElement) node;
XmlElement newElement =
node.OwnerDocum ent.CreateEleme nt(qualifiedNam e, namespaceURI);
while (oldElement.Has Attributes) {

newElement.SetA ttributeNode(ol dElement.Remove AttributeNode(o ldElement.Attri butes[0]));
}
while (oldElement.Has ChildNodes) {
newElement.Appe ndChild(oldElem ent.FirstChild) ;
}
if (oldElement.Par entNode != null) {
oldElement.Pare ntNode.ReplaceC hild(newElement , oldElement);
}
return newElement;
}
else {
return null;
}
}
}

The example I have tested that one is

<?xml version="1.0" encoding="UTF-8"?>
<root>
<element att1="value 1" att2="value ">
<child />
<god name="Kibo" />
</element>
</root>

the output is

<?xml version="1.0" encoding="UTF-8"?><root><elem ent att1="value 1"
att2="value"><c hild /><god name="Kibo" /></element></root>
<?xml version="1.0" encoding="UTF-8"?><root><n ew-element att1="value 1"
att2="value "><child /><god name="Kibo" /></new-element></root>

so the element is renamed and attributes and child nodes have been
preserved that way.

The W3C DOM Level 3 Core module suggests a method named renameNode at
http://www.w3.org/TR/DOM-Level-3-Cor...nt3-renameNode
but that is not implemented in .NET, not even in the .NET 2.0 beta as
far as I can tell.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2075
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves renaming functions etc... I came across some recursive problems. (a basic construct can be found under the section BASIC CODE) These problems do not occur when renaming objects. (see section EXTRA CODE)
3
3216
by: Ray Tayek | last post by:
hi, trying to use an xslt to make an xslt. trying something like: <?xml version="1.0" encoding="UTF-8"?> <?xmlspysamplexml H:\java\projects\spy1\spy\inputDocumentMap.xml?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/inputDocumentMap"> <xsl:element name="xsl:template" match="/inputDocument">...
5
5278
by: Adam Barr | last post by:
I have a tag foo that I want to copy unchanged when it is a subtag of bar, so I have a template (x is the namespace for the document): <xsl:template match="x:bar/x:foo"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> BUT, I discovered that someone has been mis-speling foo as foop in the
0
1737
by: Saradhi | last post by:
Hi All, Here I am facing a performance problem with the TreeView Node renaming. I am displaying a hierarchy Data in a treeview in my Windows C# Application. My tree view represents an hierarchical view of Parent Nodes and projects where in a projectnode can be added to any ParentNode and hence we may have a project node added to 100 Parent nodes. In this one, I have an operation of Renaming a Project Node. So whenever I am doing the...
6
3415
by: Martin Plantec | last post by:
Hi again, If I may, I have another, slightly more complex, XSLT question. (I'm learning, but it's not as easy as I would have thought!) I would like a rule that says: for every element that has an attribute "webclass", rename the attribute as "class", keeping the same value for the attribute (and keeping the same element name). In other words, I would like to rename an attribute, whatever the element it comes in. I gather it is...
9
3718
by: Jiho Han | last post by:
Suppose I have an xml fragment like: <mother> <child name="Bob" sex="M"/> <child name="Jane" sex="F"/> ... </mother> If I wanted to replace the <mother> element to <father> element, what is the fastest or most efficient way to do this?
0
1351
by: Andy | last post by:
Hello: I am using System.Web.Mail.MailMessage. Currently: when I am attaching a file to the object, I am renaming the file using the FileInfo.Copy method and attaching the new file to the email: sNewFileName = fPath + "Front" + fInfo.Extension; fInfo.CopyTo(sNewFileName, true); fInfo.Delete(); oMail.Attachments.Add(new MailAttachment(sNewFileName));
8
2855
by: BillCo | last post by:
I'm updating a legacy app with table naming that makes baby jesus cry. It's a bit of a spider web though... no telling when and where the tables will be called by name. So I wrote this for renaming tables (I guess it would work for queries also). It changes the table name, changes the table name, queries that refer to it, form and report control sources, combo and list box row sources etc. Anyone inspired enough to figure out how to make...
1
1498
by: Jack Maxwell | last post by:
Hope someone can help here. My brother has asked me if I can assist in renaming about 1500 midi files which are associated with a programm called Cubase SE. With that software installed he can click on the file Cm9146.mid and on the Properties tab there is details tab which lists various properties the one he wants is Sequence Name the actual song name e.g. 'Penny Lane'. What he wants me to do (if I can) is rename the .mid file to be...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8860
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7392
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5432
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3947
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.