471,339 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to add new attribute to XmlNode

I am reading an XmlFile using XmlDocument and traverse through the
XmlNode, as I read I need to append an attribute to the XmlNode on
some conditions.

I tried xmlNode.Attributes.Append()

It takes only XmlAttribute and in this when I create a new
XmlAttribute, it doesn't allow me to set the name and value as the
name is only the readonly value. How can I achive this?

Jun 27 '08 #1
4 13154
On May 12, 3:55*pm, CSharper <cshar...@gmx.comwrote:
I am reading an XmlFile using XmlDocument and traverse through the
XmlNode, as I read I need to append an attribute to the XmlNode on
some conditions.

I tried xmlNode.Attributes.Append()

It takes only XmlAttribute and in this when I create a new
XmlAttribute, it doesn't allow me to set the name and value as the
name is only the readonly value. How can I achive this?
According to:

http://www.omegacoder.com/?p=102

it can be done.

Google is your friend.
Jun 27 '08 #2
Hi CSharper,

First create the attribute, like xmldoc.CreateAttribute("newattribute"),
update it and, then, add it by CreateChild.

Regards, Alex
[TechBlog] http://devkids.blogspot.com

CI am reading an XmlFile using XmlDocument and traverse through the
CXmlNode, as I read I need to append an attribute to the XmlNode on
Csome conditions.
C>
CI tried xmlNode.Attributes.Append()
C>
CIt takes only XmlAttribute and in this when I create a new
CXmlAttribute, it doesn't allow me to set the name and value as the
Cname is only the readonly value. How can I achive this?
C>
Jun 27 '08 #3
Well, you can only set attributes on elements, so I assume your
XmlNode is actually an XmlElement (XmlElement is a more-specific
XmlNode implementation).

As such, the easiest way to handle attributes is to cast your node to
an element:

XmlNode node = {TODO};
XmlElement el = (XmlElement)node;
el.SetAttribute("Foo", "Bar");

Alternatively (leaving as XmlNode):

XmlNode node = {TODO};
XmlAttribute attrib =
node.OwnerDocument.CreateAttribute("Foo");
attrib.Value = "Bar";
node.Attributes.Append(attrib);

Marc
Jun 27 '08 #4
Heh, had a mistake in the last msg, sorry, no CreateChile for node. Correct
way:
1. Create the attribute: XmlAttribute attr = doc.CreateAttribute("newattribute");
2. Append one: xmlNode.Attributes.Append(attr);

The value can be set after both steps.

Regards, Alex
[TechBlog] http://devkids.blogspot.com

AMHi CSharper,
AM>
AMFirst create the attribute, like
AMxmldoc.CreateAttribute("newattribute"), update it and, then, add it
AMby CreateChild.
AM>
AMRegards, Alex
AM[TechBlog] http://devkids.blogspot.com
C>I am reading an XmlFile using XmlDocument and traverse through the
C>XmlNode, as I read I need to append an attribute to the XmlNode on
C>some conditions.
C>>
C>I tried xmlNode.Attributes.Append()
C>>
C>It takes only XmlAttribute and in this when I create a new
C>XmlAttribute, it doesn't allow me to set the name and value as the
C>name is only the readonly value. How can I achive this?
C>>
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Robert Strickland | last post: by
1 post views Thread by andrej | last post: by
2 posts views Thread by Josema | last post: by
3 posts views Thread by Andy | last post: by
7 posts views Thread by Simon Hart | last post: by
reply views Thread by =?Utf-8?B?ZGF2aWQ=?= | last post: by
2 posts views Thread by =?iso-8859-1?Q?Norbert_P=FCrringer?= | last post: by
reply views Thread by rosydwin | last post: by

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.