[英]Issue with adding a new node in XML
I am trying to add a new node in my XML file, but I get InvalidOperationException due to the current position of the navigator. 我试图在XML文件中添加一个新节点,但是由于导航器的当前位置,我收到InvalidOperationException。
This is my XML file: 这是我的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<dictionary xmlns="RecnikSema.xsd">
<sentiments>
<sentiment word="napustiti">-2</sentiment>
</sentiments>
</dictionary>
and schema: 和架构:
<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>
The code in C# which I am using to add a new node looks like this: 我用来添加新节点的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>");
The exception is occuring on the last line, on InsertAfter
. 例外发生在最后一行,在
InsertAfter
。
What am I doing wrong here? 我在这里做错了什么?
In MoveToChild()
, the second argument is the XML namespace, not the location of your document. 在
MoveToChild()
,第二个参数是XML名称空间,而不是文档的位置。 In your case, you have set xmlns="RecnikSema.xsd"
. 在您的情况下,您已设置
xmlns="RecnikSema.xsd"
。 This means MoveToChild
cannot find a match, so when you get to insertAfter
, the current node is still the root node <dictionary>
, and it attempts to create XML like this: 这意味着
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>
This has 2 root elements and so you get the error 它有2个根元素,所以会出现错误
Instead you need to pass "RecnikSema.xsd"
as the argument.: 相反,您需要传递
"RecnikSema.xsd"
作为参数。
navigator.MoveToChild("dictionary", "RecnikSema.xsd");
navigator.MoveToChild("sentiments", "RecnikSema.xsd");
navigator.MoveToChild("sentiment", "RecnikSema.xsd");
I'm not sure you meant to set this as the namespace as it is the Schema file, so maybe you mean this?: 我不确定您是否打算将其设置为名称空间,因为它是Schema文件,所以也许您是这个意思?:
XML 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# 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>");
I think your problem is that you did not specify the maxOccurs (default is 1) and you allready added on element. 我认为您的问题是您未指定maxOccurs(默认值为1),并且已经添加到element上。 See http://www.w3schools.com/schema/el_sequence.asp
参见http://www.w3schools.com/schema/el_sequence.asp
maxOccurs Optional.
maxOccurs可选。 Specifies the maximum number of times the sequence element can occur in the parent element.
指定序列元素可以在父元素中出现的最大次数。 The value can be any number>= 0, or if you want to set no limit on the maximum number, use the value "unbounded".
该值可以是大于等于0的任何数字,或者如果您不希望限制最大数目,请使用值“ unbounded”。 Default value is 1
预设值为1
so your Solution for multiple sentiments: 因此,您针对多种情感的解决方案:
<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>
I would prefer using Microsoft xsd tool to generate a CLR Class -> http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx and using XMLSerializer -> http://msdn.microsoft.com/de-de/library/system.xml.serialization.xmlserializer(v=vs.110).aspx 我希望使用Microsoft xsd工具生成CLR类-> http://msdn.microsoft.com/zh-cn/library/x6c1kb0s(v=vs.110).aspx并使用XMLSerializer-> http:// msdn .microsoft.com / de-de / library / system.xml.serialization.xmlserializer(v = vs.110).aspx
Why don't you make it simple mate by using XDocument. 您为什么不通过使用XDocument使其简单匹配。
The new version of C# has got this class that makes it easy to manipulate Xml. C#的新版本提供了此类,可以轻松操作Xml。 Thus, it also supports Xml Linq as well.
因此,它也支持Xml Linq。
Here is the quick solution that may be useful for you. 这是可能对您有用的快速解决方案。
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");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.