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
而你的節點數總是爲零,因爲在加載文件之前你正在計數...... –