Solved

cannot append to the XML node.

Posted on 2004-08-27
1
2,201 Views
Last Modified: 2008-01-09
Hi folks:

I am having the following error out of the third line.

System.InvalidOperationException: The current node cannot contain other nodes.

XmlNode appt = CreateParameter("XML_DATA","",false,"");
appt.AppendChild(CreateNode("APPOINT_REQUEST"));
appt.FirstChild.AppendChild(CreateNode("FAMILYNO",familyNo,false));

Can't I just append to the first child of the node that I already appended. Any ideas????

Following are the definition of other functions used:


protected XmlElement CreateNode(string elementName)
{
  XmlElement node = doc.CreateElement(elementName);
  return node;
}

protected XmlElement CreateNode(string elementName,string elementValue,bool isCDATA)
{
 XmlElement node = doc.CreateElement(elementName);
                  
 // create a CDATA elemetn in here
 if (isCDATA )
 {
  XmlCDataSection cDATA = doc.CreateCDataSection(elementValue);
  node.AppendChild(cDATA);
 }
 else
  node.InnerText = elementValue;
 return node;
}


protected XmlElement CreateParameter(string name,string data,bool isCDATA,string type)
{
 XmlElement node = doc.CreateElement("PARAM");
            
 XmlAttribute attribute      = doc.CreateAttribute("NAME");
 attribute.InnerText            = name;
 node.Attributes.Append(attribute);

 attribute  = doc.CreateAttribute("TYPE");
 attribute.InnerText = type;
 node.Attributes.Append(attribute);
 
 if (isCDATA)
 {
   XmlCDataSection cDATA = doc.CreateCDataSection(data);
  node.AppendChild(cDATA);      
 }
 else
 {
  node.InnerText = data;      
 }
      
 return node;
}


0
Comment
Question by:rxraza
1 Comment
 
LVL 19

Accepted Solution

by:
drichards earned 125 total points
ID: 11916310
You have a subtle problem here.  In your CreateParameter method, you set "node.InnerText".  This creates a text node under the element node.  Then when you do "appt.FirstChild" you are referencing the text node (which cannot have children) and NOT the "APPOINT_REQUEST" element as you think.  You have several options.

1) Don't add the text node to the appt node - this is still fragile
2) Save the "APPT_REQUEST" node:

        XmlNode appt = CreateParameter("XML_DATA","",false,"");
        appt.AppendChild(CreateNode("APPOINT_REQUEST"));
        XmlNode node = CreateNode("APPOINT_REQUEST");
        appt.AppendChild(node);
        node.AppendChild(CreateNode("FAMILYNO",familyNo,false));

3) Explicitly select the node you want to append to:

        XmlNode appt = CreateParameter("XML_DATA","",false,"");
        appt.AppendChild(CreateNode("APPOINT_REQUEST"));
        XmlNode node = appt.SelectSingleNode("/APPOINT_REQUEST");
        node.AppendChild(CreateNode("FAMILYNO","My family",false));
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

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 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

22 Experts available now in Live!

Get 1:1 Help Now