473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

CreateElement question

Hi,
I am trying to add an XmlElement to my XML file:
XmlDocument aDoc = new Xml Document();
aDoc.Load("MyFile.xml");
XmlElement el = aDoc.CreateElement("Cim:Attr");

When I append this element to a node, the name of element is "Attr" only.

How can I add an XmlElement and recieve the full name (Cim:Attr) ?

Yoav.


Nov 1 '06 #1
2 1916
Yoavo wrote:
I am trying to add an XmlElement to my XML file:
XmlDocument aDoc = new Xml Document();
aDoc.Load("MyFile.xml");
XmlElement el = aDoc.CreateElement("Cim:Attr");

When I append this element to a node, the name of element is "Attr" only.

How can I add an XmlElement and recieve the full name (Cim:Attr) ?
You need to know the namespace URL you want to create the element in,
just a prefix does not suffice, if you know the URL then you can use e.g.
XmlElement el = aDoc.CreateElement("Cim", "Attr", "http://example.com/");

As you already load an existing XML document from a file the prefix Cim
might be bound to a URL in that document so depending on what you want
to achieve
XmlElement el = aDoc.CreateElement("Cim", "Attr",
aDoc.DocumentElement.GetNamespaceOfPrefix("Cim"));
might do (without any need to name the URL in your code literally).
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 1 '06 #2
There are two steps involved in creating a new XML element for an
existing XML document.

First, you have to create the element itself inside the document.

Then, you have to "activate" the element by attaching it to another
node within the document.

To minimize your coding, you may want to create a common routine that
does both steps in a single call. The following takes a reference to
the node you want to attach the element to in the document, the name of
the new element, the data it will hold, and a flag that controls
whether the data should be encapsulated with CDATA tags. If the
element already exists, then only its value is updated. A reference to
the newly created element is returned.

public XmlNode AddElement(XmlNode parentNode,
System.String elementName,
System.String data,
System.Boolean blnCDATA)
{
XmlNode newNode = parentNode.OwnerDocument.CreateElement(elementName );
if(blnCDATA==true)
{
XmlCDataSection cdataNode =
parentNode.OwnerDocument.CreateCDataSection(data);
newNode.AppendChild(cdataNode);
}else{
newNode.InnerText = data;
}
parentNode.AppendChild(newNode);
return(newNode);
}//AddElement

You can also do something similar for attributes. This example takes a
reference to the node you want to add the attribute to, a name for the
new attribute and the value it contains. If the attribute already
exists, it is updated instead. A reference to the attribute is
returned.

public XmlAttribute AddAttribute(XmlNode parentNode,
System.String attributeName,
System.String attributeValue)
{
XmlAttribute newAttribute =
parentNode.OwnerDocument.CreateAttribute(attribute Name);
newAttribute.InnerText = attributeValue;
parentNode.Attributes.SetNamedItem(newAttribute);

And, you can also do the same thing for whole branches of nodes within
XML documents. This example accepts a reference to a node in any XML
document that will have the branch added to it, and another reference
of a node in any other XML document that is the root of the branch you
want to add (note that only an entire copy is made of the branch; it is
not moved nor does the parentNode contain a reference to the original
branch).

public XmlNode AddXML(XmlNode parentNode, XmlNode newBranch)
{
System.String newBranchName = newBranch.Name;
System.String parentName=parentNode.Name;
System.String XPath = "//" + parentName + "/" + newBranchName;

System.Xml.XmlNode newData =
parentNode.OwnerDocument.ImportNode(newBranch,true ); //do a deep copy
of the new data

if(parentNode.SelectSingleNode(XPath)!=null)
{
//there's already a newBranch in the document, so replace it
System.Xml.XmlNode
oldData=parentNode.OwnerDocument.SelectSingleNode( XPath);
parentNode.OwnerDocument.DocumentElement.ReplaceCh ild(newData,oldData);
}
else
{
//the newBranch doesn't exist in the document, so add it
parentNode.OwnerDocument.DocumentElement.AppendChi ld(newData);
}
return(newData);
}//AddXML
return(newAttribute);
}//AddAttribute

Andy

Nov 1 '06 #3

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

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
2
by: kie | last post by:
hello, when i create elements and want to assign events to them, i have realised that if the function assigned to that element has no parameters, then the parent node values can be attained. ...
4
by: sg_maat | last post by:
I have a little problem with createElement(and msie). I was experimenting a bit with createElement and made the following script: <script> var tmp = document.createElement("div");...
9
by: Kim | last post by:
Hi I'm trying to do something like this. for(i=0; i<5; i++){ var theData = document.createElement('a'); theData.onmouseover = function() { return get(i);} ... ... }
7
by: bayfaulkscatering | last post by:
I'm definitely not new to JS, but for the life of me, I can't figure this one out. Here's basically what I'm doing: function foo() { alert(this); } span = document.createElement('span'); a...
3
by: Arpan | last post by:
Using DOM, this is how I am appending data to an existing XML file which is named AppendData.xml (the root element of AppendData.xml is <ProductList>): <script runat="server"> Sub...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
2
by: TopherB | last post by:
Okay, I think I can simplify my earlier question. If I have a html form <form> <input type="button" value="More" onclick="addField(this.form;return false; )"> </form> and assuming this...
7
by: Tarik Monem | last post by:
Why am I having so much trouble with using DOM in IE & Opera to create, then remove an Object & Embedded element? Here's the code that works in Firefox (Mac/Win/Linux) and Safari, but not on IE or...
23
by: vunet | last post by:
It is recommended by some sources I found to create IFrames in IE using document.createElement('<iframe src="#">') instead of document.createElement('iframe'). Why and what browser versions to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.