用XML添加新节点的问题
我想在我的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
。
我在这里做错了什么?
为什么不要通过使用XDocument来简化队友。
C#的新版本已经有了这个类,可以很容易地操纵Xml。因此,它也支持Xml Linq。
以下是可能对您有用的快速解决方案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XDocument document = XDocument.Load(@"C:\Users\amit\SkyDrive\code\WebApplication1\ConsoleApplication1\xml.xml");
XElement root = new XElement("sentiment");
root.Value = "3";
root.Add(new XAttribute("word", "napustiti"));
XNamespace nsSys = "RecnikSema.xsd";
document.Element(nsSys + "dictionary").Element(nsSys + "sentiments").Add(root);
document.Save("c:\newFile.xml");
}
}
}
在MoveToChild()
中,第二个参数是XML名称空间,而不是文档的位置。在你的情况下,你已经设置了xmlns="RecnikSema.xsd"
。这意味着MoveToChild
不能找到匹配,那么当你到insertAfter
,当前节点还是根节点<dictionary>
,并试图像这样创建XML:
<?xml version="1.0" encoding="utf-8" ?>
<dictionary xmlns="RecnikSema.xsd">
<sentiment word="napustiti">-2</sentiment>
</dictionary>
<sentiment word="foo">5</sentiment>
这有2种元素等你拿错误
相反,你需要通过"RecnikSema.xsd"
作为参数:
navigator.MoveToChild("dictionary", "RecnikSema.xsd");
navigator.MoveToChild("sentiments", "RecnikSema.xsd");
navigator.MoveToChild("sentiment", "RecnikSema.xsd");
我不知道你的意思是设置这是因为它是架构文件的命名空间,所以也许你的意思呢? :
XML
<?xml version="1.0" encoding="utf-8" ?>
<dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="RecnikSema.xsd">
<sentiments>
<sentiment word="napustiti">-2</sentiment>
</sentiments>
</dictionary>
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", "");
navigator.MoveToChild("sentiments", "");
navigator.MoveToChild("sentiment", "");
navigator.InsertAfter("<sentiment word=\"" + token + "\">" + value + "</sentiment>");
我觉得你的问题是,你没有指定的maxOccurs(默认为1),你媒体链接元素的添加。请参阅http://www.w3schools.com/schema/el_sequence.asp
maxOccurs可选。指定元素在父元素中可能发生的最大次数 。该值可以是任意数字> = 0,或者如果您不想限制最大数量,请使用值“无界”的值 。默认值为1
所以你多情绪的解决方案:
<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 maxOccurs="unbounded">
<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>
我宁愿使用Microsoft XSD工具来生成CLR类 - >http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx和使用的XMLSerializer - >http://msdn.microsoft.com/de-de/library/system.xml.serialization.xmlserializer(v=vs.110).aspx
嘿马里奥,这只是我在发布时在变量和属性翻译过程中犯的一个错误。 因此,InvalidOperationException异常,解释如下: System.Xml.dll中发生未处理的类型为“System.InvalidOperationException”的异常 附加信息:由于导航器的当前位置,操作无效。 – LukaVazic 2014-09-24 11:36:40