How to add new attribute to XmlNode

C

CSharper

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?
 
A

Alex Meleta

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



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>
 
Z

zacks

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.
 
M

Marc Gravell

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
 
A

Alex Meleta

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



AM> Hi CSharper,
AM>
AM> First create the attribute, like
AM> xmldoc.CreateAttribute("newattribute"), update it and, then, add it
AM> by CreateChild.
AM>
AM> Regards, 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>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top