473,396 Members | 2,076 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,396 software developers and data experts.

Adding attributes to an element

fix
Hi,

I have an XML document and I need to add some attributes to an element.
I use the XMLDocument object to load the xml file, and I can get the
element I need to add attributes to in an XMLNode. The problem is, it
seems that I cannot create a new XmlAttribute, it has no constructor.
Does anyone have any idea how I can do this? I would appreciate your
help. Thanks.

fix.

Nov 12 '05 #1
8 3225
fix wrote:
I have an XML document and I need to add some attributes to an element.
I use the XMLDocument object to load the xml file, and I can get the
element I need to add attributes to in an XMLNode. The problem is, it
seems that I cannot create a new XmlAttribute, it has no constructor.


What about XmlElement.SetAttribute() method?

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.xmllab.net
http://blog.tkachenko.com
Nov 12 '05 #2
Hello!
I have an XML document and I need to add some attributes to an element.
I use the XMLDocument object to load the xml file, and I can get the
element I need to add attributes to in an XMLNode. The problem is, it
seems that I cannot create a new XmlAttribute, it has no constructor.
Does anyone have any idea how I can do this? I would appreciate your
help. Thanks.


You don't use the constructors of XmlNode (and classes that inherit from
it). You have to create the objects using the
XmlDocument.CreateXXX-Methods (like CreateTextNode, CreateElement,...)
and then add them to the Tree.
--
Pascal Schmitt
Nov 12 '05 #3
fix
Oh yes, XmlElement object works for me. I was just looking into
XmlNode...... But what's the different between this two? Is XmlElement
only for a "tag" but XmlNode can point to "tags", attributes, etc?

Thanks.
fix.

Oleg Tkachenko [MVP] wrote:
fix wrote:
I have an XML document and I need to add some attributes to an element.
I use the XMLDocument object to load the xml file, and I can get the
element I need to add attributes to in an XMLNode. The problem is, it
seems that I cannot create a new XmlAttribute, it has no constructor.

What about XmlElement.SetAttribute() method?


Nov 12 '05 #4
fix
Pascal Schmitt wrote:
Hello!
I have an XML document and I need to add some attributes to an element.
I use the XMLDocument object to load the xml file, and I can get the
element I need to add attributes to in an XMLNode. The problem is, it
seems that I cannot create a new XmlAttribute, it has no constructor.
Does anyone have any idea how I can do this? I would appreciate your
help. Thanks.

You don't use the constructors of XmlNode (and classes that inherit from
it). You have to create the objects using the
XmlDocument.CreateXXX-Methods (like CreateTextNode, CreateElement,...)
and then add them to the Tree.


I wasn't too sure about the difference between XmlNode and XmlElement. I
am now using XmlElement.SetAttribute and it works. Anyway, thanks.

fix.

Nov 12 '05 #5


fix wrote:
Oh yes, XmlElement object works for me. I was just looking into
XmlNode...... But what's the different between this two? Is XmlElement
only for a "tag" but XmlNode can point to "tags", attributes, etc?


Well, XmlElement is for element nodes, XmlAttribute is for attribute
nodes, XmlText is for text nodes and so on. XmlNode is a common base
class for all those type of nodes. Thus each instance of XmlElement is
also an instance of XmlNode but not the other way round.

The markup of elements consists e.g. of start and end "tag" and the
contents in between
<god>Kibo</god>
but in the object model there is nothing called "tags", there are nodes
of different types where one type is XmlElement for element nodes.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #6
XmlDocument xmlDoc = new XmlDocument();
XmlNode xmlNode;
// .. some code to create XML doc
xmlNode = xmlDoc.SelectSingleNode("<some XPath expression>");
xmlChild = (XmlElement)xmlNode;
xmlChild.SetAttribute("name", "value");

HTH,
Stefan

"fix" <fi*@here.com> schrieb im Newsbeitrag
news:eh*************@tk2msftngp13.phx.gbl...
Hi,

I have an XML document and I need to add some attributes to an element.
I use the XMLDocument object to load the xml file, and I can get the
element I need to add attributes to in an XMLNode. The problem is, it
seems that I cannot create a new XmlAttribute, it has no constructor.
Does anyone have any idea how I can do this? I would appreciate your help.
Thanks.

fix.

Nov 12 '05 #7
Hello!
Oh yes, XmlElement object works for me. I was just looking into
XmlNode...... But what's the different between this two? Is XmlElement
only for a "tag" but XmlNode can point to "tags", attributes, etc?


Well, XmlElement is for element nodes, XmlAttribute is for attribute
nodes, XmlText is for text nodes and so on. XmlNode is a common base
class for all those type of nodes. Thus each instance of XmlElement is
also an instance of XmlNode but not the other way round.

The markup of elements consists e.g. of start and end "tag" and the
contents in between
<god>Kibo</god>
but in the object model there is nothing called "tags", there are nodes
of different types where one type is XmlElement for element nodes.


This is an interesting introduction (and a core reference...):

<http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/introduction.html>
--
Pascal Schmitt

Nov 12 '05 #8
fix
Pascal Schmitt wrote:
Hello!
Oh yes, XmlElement object works for me. I was just looking into
XmlNode...... But what's the different between this two? Is
XmlElement only for a "tag" but XmlNode can point to "tags",
attributes, etc?

Well, XmlElement is for element nodes, XmlAttribute is for attribute
nodes, XmlText is for text nodes and so on. XmlNode is a common base
class for all those type of nodes. Thus each instance of XmlElement is
also an instance of XmlNode but not the other way round.

The markup of elements consists e.g. of start and end "tag" and the
contents in between
<god>Kibo</god>
but in the object model there is nothing called "tags", there are
nodes of different types where one type is XmlElement for element nodes.

This is an interesting introduction (and a core reference...):

<http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/introduction.html>


All right guys, thanks a lot. Now I have a clearer picture.

fix.

Nov 12 '05 #9

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

Similar topics

1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
2
by: Jordan Willms | last post by:
Hi there. I am working with lom metadata and I am a little confused with how to form the following xml element: <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2"...
6
by: Amir Hardon | last post by:
I'm new to DOM and can't figure out this thing: I'm trying to add a row to a table with a form field in one of it's cells, but if I'm appending the field to a form it gets out of the table. Can...
10
by: Melissa Mussitsch | last post by:
I've done this before while creating a brand new table. But the code below is not working and I keep getting the error: "Internet Explorer cannot open the Internet site ..... Operation aborted" ...
2
by: Maersa | last post by:
hi, i'm trying to add "attributes" to some nodes and it works at design time but fails at runtime. XmlDocument doc = new XmlDocument(); XmlElement elem = doc.CreateElement( "root" );...
8
by: Ian Collins | last post by:
Is there a way to do this in IE? In Firefox, one can simply append a node from an XML return to the current document. IE whinges about 'No such interface supported' -- Ian Collins.
2
by: Muzzy | last post by:
Hi, I've used information on these newsgroups to build many pages. So I thought that now that I have my script working (something that I've been working on for about a week), I should post it so...
5
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
11
by: paul.boparai | last post by:
Hi, I have an element that I would like to add to the docbook DTD. This element is a wrapper element that I would like to wrap all of the standard docbook elements. This element is called...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.