Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cannot append to the XML node.

Posted on 2004-08-27
1
Medium Priority
?
2,338 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 500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

730 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