Connecting Tech Pros Worldwide Forums | Help | Site Map

Add attribute to node created using CreateElement

Anita C
Guest
 
Posts: n/a
#1: Nov 11 '05
Hi,
How can I add an attribute (called tag) to a node that I've added to an xml
document using the CreateElement method.
My code is as follows:
XmlDocument doc1 = new XmlDocument();

XmlDeclaration dec = doc1.CreateXmlDeclaration("1.0", "", "yes");

doc1.PrependChild ( dec );

XmlNode cont = doc1.CreateElement("Container");

doc1.AppendChild(cont);

Thanks in advance.



Anita C
Guest
 
Posts: n/a
#2: Nov 11 '05

re: Add attribute to node created using CreateElement


Hi,
I don't see 'SetAttric=bute' in the intellisense list, however inspite of
that if i add the code specified by you and compile the project I get the
following error - System.xml.xmlnode does not contain a definition for
setattribute. Should I add any assembly reference(I am using VS 1.1)
Thanks in advance.

"Oleg Tkachenko" <olegt@multiconn.com> wrote in message
news:%23r3IVC$PDHA.2160@TK2MSFTNGP11.phx.gbl...[color=blue]
> Anita C wrote:
>[color=green]
> > How can I add an attribute (called tag) to a node that I've added to an[/color][/color]
xml[color=blue][color=green]
> > document using the CreateElement method.
> > My code is as follows:
> > XmlDocument doc1 = new XmlDocument();
> >
> > XmlDeclaration dec = doc1.CreateXmlDeclaration("1.0", "", "yes");
> >
> > doc1.PrependChild ( dec );
> >
> > XmlNode cont = doc1.CreateElement("Container");[/color]
> XmlElement cont = doc1.CreateElement("Container");
> cont.SetAttribute("tag", "foo");[color=green]
> >
> > doc1.AppendChild(cont);[/color]
>
> --
> Oleg Tkachenko
> http://www.tkachenko.com/blog
> Multiconn Technologies, Israel
>[/color]


Closed Thread