2
我正在尝试使用ASP.NET在XML中存储一些数据。这是我的XML文件。修改XmlDocument时出现InvalidOperationException
<?xml version="1.0" encoding="utf-8" ?>
<SkillsInformation>
<Details id="1">
<Name>XML</Name>
<Description>Fundamentals of XML</Description>
</Details>
<Details id="2">
<Name>Java</Name>
<Description>Fundamentals of Java</Description>
</Details>
</SkillsInformation>
我要插入的技巧,但我得到和错误的说法,
An exception of type 'System.InvalidOperationException' occurred in System.Xml.dll but was not handled in user code.
{"The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type."}
这里是我创建的Details
元素,并添加属性id
。
XmlDocument xmlDoc = new XmlDocument();
//Get the nodes
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Details");
//Counting nodes to get count of the skill items
idCount = nodeList.Count;
xmlDoc.Load(Server.MapPath("skills.xml"));
XmlElement parentElement = xmlDoc.CreateElement("Details");
//xmlDoc.AppendChild(parentElement);
String attributeValue = idCount++.ToString();
XmlAttribute idAttribute = xmlDoc.CreateAttribute("id", attributeValue);
//idAttribute.Value = attributeValue;
parentElement.Attributes.Append(idAttribute);
XmlAttribute nameElement = xmlDoc.CreateAttribute("Name");
nameElement.InnerText = name.Text;
XmlAttribute descriptionElement = xmlDoc.CreateAttribute("Description");
descriptionElement.InnerText = description.Text;
parentElement.AppendChild(nameElement);
parentElement.AppendChild(descriptionElement);
//xmlDoc.AppendChild(parentElement);
xmlDoc.DocumentElement.AppendChild(parentElement);
bindData();
名称和描述不是属性;属性不是子元素。 – CodeCaster
为了将来的参考,如果您告诉我们在哪一行发生了异常,这将会很有帮助。 – JLRishe
而你的节点数总是为零,因为在加载文件之前你正在计数...... –