Home All Groups Group Topic Archive Search About

XMLNode --> Adding an Attribute

Author
28 Apr 2006 8:00 PM
Andy
Hello Guys:

What am I doing wrong with this code?  I can't seem to get it to simply add
an attribute to my node.  The node already exists.  I am simply opening the
XMLDocument and creating one attribute to the document.  I am not creating
the document new

XmlNamespaceManager xnsm = new XmlNamespaceManager(xmlFile.NameTable);

xnsm.AddNamespace("a", "http://www.icsm.com/icsmxml");
XmlNode xNode = xmlFile.SelectSingleNode("//a:HeaderShipping", xnsm);

System.Xml.XmlAttribute attribute;
attribute..Name = "accountNumber";
attribute.Value = thirdPartyShipping;

xNode.Attributes.Append(attribute);

Thanks
Andy

Author
28 Apr 2006 8:06 PM
Marina Levit [MVP]
You didn't say what happeens? Error? No error? If error, what is the
message? Which line is causing the problem?
If no error, what exactly happens vs. what do you expect?

These are all important details!

In any case, your 'attribute' variable is never getting instantiated. So I
would assume the line where you assign the Name property is where you get a
NullReferenceException. That is, if you really only used one dot and not two
when referencing the Name property, otherwise it wouldn't have compiled.

Show quoteHide quote
"Andy" <A***@discussions.microsoft.com> wrote in message
news:12DF5387-DC0E-45C7-BC50-108AD6D68124@microsoft.com...
> Hello Guys:
>
> What am I doing wrong with this code?  I can't seem to get it to simply
> add
> an attribute to my node.  The node already exists.  I am simply opening
> the
> XMLDocument and creating one attribute to the document.  I am not creating
> the document new
>
> XmlNamespaceManager xnsm = new XmlNamespaceManager(xmlFile.NameTable);
>
> xnsm.AddNamespace("a", "http://www.icsm.com/icsmxml");
> XmlNode xNode = xmlFile.SelectSingleNode("//a:HeaderShipping", xnsm);
>
> System.Xml.XmlAttribute attribute;
> attribute..Name = "accountNumber";
> attribute.Value = thirdPartyShipping;
>
> xNode.Attributes.Append(attribute);
>
> Thanks
> Andy
Are all your drivers up to date? click for free checkup

Author
28 Apr 2006 8:30 PM
Ole Nielsby
Andy <A***@discussions.microsoft.com> wrote:

> Hello Guys:
>
> What am I doing wrong [...]

> System.Xml.XmlAttribute attribute;
> attribute..Name = "accountNumber";
> attribute.Value = thirdPartyShipping;

You need to understand class instances and their creation better.

Declaring a variable of a class type (System.Xml.XmlAttribute)
doesn't create or allocate an object of that type. It only allocates
a reference (which is essentially a pointer that is traced by the
garbage collector) and initializes it with a null reference. You
need to create the object with 'new', as specified by the constructor
which is documented in the class library reference.

HTH/ONI
Author
29 Apr 2006 12:25 PM
Martin Honnen
Andy wrote:


> What am I doing wrong with this code?  I can't seem to get it to simply add
> an attribute to my node.  The node already exists.  I am simply opening the
> XMLDocument and creating one attribute to the document.  I am not creating
> the document new
>
> XmlNamespaceManager xnsm = new XmlNamespaceManager(xmlFile.NameTable);
>
> xnsm.AddNamespace("a", "http://www.icsm.com/icsmxml");
> XmlNode xNode = xmlFile.SelectSingleNode("//a:HeaderShipping", xnsm);

The simplest approach is doing e.g.
   XmlElement xNode = xmlFile.SelectSingleNode("//a:HeaderShipping",
xnsm) as XmlElement;
   xNode.SetAttribute("accountNumber", thirdPartyShipping);

If you really think you need to create the attribute node separately
then use the factory method the XmlDocument instance itself exposes e.g.
   XmlAttribute attribute = xmlDocument.CreateAttribute("accountNumber");
   attribute.Value = thirdPartyShipping;
   XmlElement xNode = xmlFile.SelectSingleNode("//a:HeaderShipping",
xnsm) as XmlElement;
   xNode.SetAttributeNode(attribute);



--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Bookmark and Share