Solved

cannot append to the XML node.

Posted on 2004-08-27
1
2,312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

615 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