Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2003

Tip: Looking for answers? Try searching our database.

how can I change node name

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zerdust - 16 Oct 2003 13:38 GMT
how can I change the name of XML node?
this is sample

<?xml version="1.0"?>
<adresboek>
  <adres>
  <naam>Mark van Renswoude</naam>
  <straat>ddden 24</straat>
  <plaats>Scddd</plaats>
   <email>ma@xe.net</email>
   </adres>
</adresboek>

I copy this sample with using Clone();
and how can I change node name overthere.

<?xml version="1.0"?>
<adresboek>
 <adres>
  <naam>Mark van Renswoude</naam>
  <straat>xxxxx 25</straat>
  <plaats>Scddd</plaats>
   <email>ma@xe.net</email>
   </adres>
 <adres_old>------------------------------change name
  <naam>Mark van Renswoude</naam>
  <straat>ddden 24</straat>
  <plaats>Scddd</plaats>
   <email>ma@xe.net</email>
   </adres_old> ----------------------------change name

</adresboek
Martin Honnen - 16 Oct 2003 14:07 GMT
> how can I change the name of XML node?
> this is sample
[quoted text clipped - 28 lines]
>
>  </adresboek>

You can't change the name of a node, rather you need to create a new
node with a different name.

Signature

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

zerdust - 16 Oct 2003 14:32 GMT
XmlDocument xml = new XmlDocument();

xml.Load("c:\x.xml");

XmlNode rootTextNode = xml.SelectSingleNode("vvt/dd");

XmlNode root = xml.SelectSingleNode("vvt");

XmlNode cloneTextNode = rootTextNode.Clone();

cloneTextNode. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<is it
possible to change name here?

root.AppendChild(cloneTextNode);

xml.Save("c:\\b.xml");

> > how can I change the name of XML node?
> > this is sample
[quoted text clipped - 36 lines]
> Martin Honnen
> http://JavaScript.FAQTs.com/
Martin Honnen - 16 Oct 2003 15:36 GMT
> XmlDocument xml = new XmlDocument();
>
[quoted text clipped - 8 lines]
> cloneTextNode. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<is it
> possible to change name here?

No, as said, you cannot simply chane the name, you need to create a new
element node with the desired name, here is an example:

using System.Xml;

public class Test20031016 {
  public static void Main (string[] args) {
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(@"test20031011.xml");
    System.Console.WriteLine(xmlDocument.OuterXml);
    XmlElement element = (XmlElement)
xmlDocument.GetElementsByTagName("god")[0];
    XmlElement newElement = copyElementToName(element, "super-god");
    xmlDocument.DocumentElement.AppendChild(newElement);
    System.Console.WriteLine(xmlDocument.OuterXml);
  }

  public static XmlElement copyElementToName (XmlElement element,
string tagName) {
    XmlElement newElement = element.OwnerDocument.CreateElement(tagName);
    for (int i = 0; i < element.Attributes.Count; i++) {
      newElement.SetAttributeNode((XmlAttribute)
element.Attributes[i].CloneNode(true));
    }
    for (int i = 0; i < element.ChildNodes.Count; i++) {
      newElement.AppendChild(element.ChildNodes[i].CloneNode(true));
    }
    return newElement;
  }
}

That function copyElementToName allows you to do what you want, if the
document is originally

<?xml version="1.0" encoding="UTF-8"?>
<gods>
  <god value="Supergod">
    <name>Kibo</name>
    <home>http://www.kibo.com/</home>
  </god>
</gods>

then the program outputs

<?xml version="1.0" encoding="UTF-8"?><gods><god
value="Supergod"><name>Kibo</na
me><home>http://www.kibo.com/</home></god><super-god
value="Supergod"><name>Kibo
</name><home>http://www.kibo.com/</home></super-god></gods>
Signature


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

Jame Healy - 30 Oct 2003 20:07 GMT
Hi Martin,

I've been working on something similar, but trying to generalize it as well.
The concept is a function that will take two parameters (XmlDocument
inboundDocument, string attributeName) and will go through the XML document
recursively renaming elements by the value of their name attribute (if it
exists).

Sample input:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <element name="Supergod">
    <element name="GodName">Kibo</element>
    <element name="GodHome">http://www.kibo.com/</element>
  </element>
</root>

Required output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <Supergod>
    <GodName>Kibo</GodName>
    <GodHome>http://www.kibo.com/</GodHome>
  </Supergod>
</root>

I'm curious how you would approach this problem.

> > XmlDocument xml = new XmlDocument();
> >
[quoted text clipped - 58 lines]
> value="Supergod"><name>Kibo
> </name><home>http://www.kibo.com/</home></super-god></gods>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.