Solved

Problem with setting the value on a node of type element in xml

Posted on 2004-10-14
4
891 Views
Last Modified: 2008-02-01
Hi there,

I am trying to add a new record to my XML file. I was able to create the record and its fields, but when I tried to add values to the fields, I got the error: "Cannot set a value on node type: Element".

I am confident that I have something missing in my code, that it is not making it recognize the ability to set the value of my elements.

Here is a sample of my code:

        XmlDocument SrcDoc = new XmlDocument();
        SrcDoc.Load(xmlfile);
        XmlNode RootNode = SrcDoc.DocumentElement;

        //create a new table node
        XmlNode Newtable = SrcDoc.CreateNode(XmlNodeType.Element, "table", "");
        //create the sub node
        XmlNode ExtNode = SrcDoc.CreateNode(XmlNodeType.Element, "Ext","");
        //fill the data in ----------------------------------my error is here
        ExtNode.Value = newext;   // being passed to the subroutine

        //add the node to the new table node
        Newtable.AppendChild(ExtNode);
        //add the new table node to the root
        RootNode.AppendChild(Newtable);
        //save the docment back to the file
        SrcDoc.Save(xmlfile);


Thank you in advance for your prompt help.

yk
0
Comment
Question by:markataya
  • 2
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
Hans Langer earned 125 total points
ID: 12312422
replace this  

   //create a new table node
        XmlNode Newtable = SrcDoc.CreateNode(XmlNodeType.Element, "table", "");
        //create the sub node
        XmlNode ExtNode = SrcDoc.CreateNode(XmlNodeType.Element, "Ext","");
        //fill the data in ----------------------------------my error is here
        ExtNode.Value = newext;   // being passed to the subroutine

to


     //create a new table node
        XmlNode Newtable = SrcDoc.CreateElement("table");
        //create the sub node
        XmlNode ExtNode = SrcDoc.CreateElement("ext");
        //fill the data in ----------------------------------my error is here
        ExtNode.InnerText= newext;   // being passed to the subroutine


GL
0
 

Author Comment

by:markataya
ID: 12319669
Thank you! It works perfectly.

Is it possible to highlight the reason why the second case with CreateElement worked, while the CreateNode did not work?

Thank you again for your prompt response.

-yk
0
 
LVL 10

Expert Comment

by:Hans Langer
ID: 12319794
the problem was just de
.value property
must be
.InnerText
Look at this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmlnodeclassvaluetopic.asp
GL
0
 

Author Comment

by:markataya
ID: 12321118
Thanks again for all the help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now