473,796 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3248
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.SetA ttribute() 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.Cre ateXXX-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.SetA ttribute() 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.Cre ateXXX-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.SetA ttribute 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.SelectSi ngleNode("<some XPath expression>");
xmlChild = (XmlElement)xml Node;
xmlChild.SetAtt ribute("name", "value");

HTH,
Stefan

"fix" <fi*@here.com > schrieb im Newsbeitrag
news:eh******** *****@tk2msftng p13.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.ht ml>
--
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.ht ml>


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
2725
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 namespace prefix, what is its namespace, if default namespaces do not apply? Are (either of) prefixed or non-prefixed attributes correct? For example, are the following equivalent: <html:br class="foo"...
2
3406
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_v1p2 imsmd_v1p2p2.xsd"/>
6
2835
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 some one tell me what I'm doing wrong? it looks like this: var tbl=document.tbl; var frm=document.frm; var newcell=document.createElement("TD");
10
1753
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" Here is the code: myBody=document.getElementsByTagName("body").item(0); mytablebody = document.getElementById('product').getElementsByTagName("TBODY" );
2
9309
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" ); elem.InnerXml = "<first>abcde</first><last>1234</last>";
8
3367
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
3116
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 that it may help others. If posting this script is against the rules in this group then please accept my appologies. I developped this script so that I can add and remove rows in a table in which I have various input fields and I would use the...
5
4603
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 to add a dictionary of name-value pairs. In other words, instead of a single name-value, I want to add a name-value collection in there. I could do this:
11
1812
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 "changebar". So for example I would like changebar to appear like: <changebar> <chapter> .........
0
9673
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
9525
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
10221
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...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
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...
0
6785
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
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.