Solved

cannot append to the XML node.

Posted on 2004-08-27
1
2,182 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

13 Experts available now in Live!

Get 1:1 Help Now