//This is in a method
//add another element (child of the root)
xmlelem2=this.CreateElement("","Person","");
XmlAttribute atrXML = xmldoc.CreateAttribute("PersonId");
atrXML.Value = "100";
xmlelem2.SetAttributeNode(atrXML);
xmlelem2.AppendChild(xmltext);
this.ChildNodes.Item(1).AppendChild(xmlelem2);
The problem is that you're appending an empty text node. If you get rid
of xmlelem2.AppendChild(xmltext); then it works fine.
Here's a short but complete example program:
using System;
using System.Xml;
class Test
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateNode
(XmlNodeType.XmlDeclaration,"",""));
XmlElement root = doc.CreateElement ("", "ROOT", "");
XmlText text = doc.CreateTextNode ("");
root.AppendChild(text);
doc.AppendChild(root);
XmlElement person = doc.CreateElement ("", "Person", "");
XmlAttribute attr = doc.CreateAttribute ("PersonId");
attr.Value = "100";
person.SetAttributeNode (attr);
// Uncomment this line to get the "broken" behaviour
// person.AppendChild(text);
root.AppendChild(person);
doc.Save(Console.Out);
}
}