我想在我的XML文件中添加新節點,但由於導航器的當前位置,我得到InvalidOperationException。用XML添加新節點的問題
這是我的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<dictionary xmlns="RecnikSema.xsd">
<sentiments>
<sentiment word="napustiti">-2</sentiment>
</sentiments>
</dictionary>
和架構:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="dictionary">
<xs:complexType>
<xs:sequence>
<xs:element name="sentiments">
<xs:complexType>
<xs:sequence>
<xs:element name="sentiment">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="word"/>
<xs:attribute type="xs:double" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
C#中的代碼,我使用添加一個新的節點如下:
XmlDocument dictionary= new XmlDocument();
dictionary.Load(@"C:\Users\Luka\Documents\Visual Studio 2013\Projects\TSA\TSA\Dictionary.xml");
XPathNavigator navigator = dictionary.CreateNavigator();
navigator.MoveToChild("dictionary", @"C:\Users\Luka\Documents\Visual Studio 2013\Projects\TSA\TSA\RecnikSema.xsd");
navigator.MoveToChild("sentiments", @"C:\Users\Luka\Documents\Visual Studio 2013\Projects\TSA\TSA\RecnikSema.xsd");
navigator.MoveToChild("sentiment", @"C:\Users\Luka\Documents\Visual Studio 2013\Projects\TSA\TSA\RecnikSema.xsd");
navigator.InsertAfter("<sentiment word=\"" + token + "\">" + value + "</sentiment>");
發生在最後一行的例外是InsertAfter
。
我在這裏做錯了什麼?
嘿馬里奧,這只是我在發佈時在變量和屬性翻譯過程中犯的一個錯誤。 因此,InvalidOperationException異常,解釋如下: System.Xml.dll中發生未處理的類型爲「System.InvalidOperationException」的異常 附加信息:由於導航器的當前位置,操作無效。 – LukaVazic 2014-09-24 11:36:40