在XML中添加新节点的问题

卢卡·瓦齐奇(LukaVazic)

我正在尝试在XML文件中添加一个新节点,但是由于导航器的当前位置,我得到了InvalidOperationException。

这是我的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");
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在XML文件中添加新节点

来自分类Dev

向Java中的现有XML节点添加新属性?

来自分类Dev

C链表添加新节点问题

来自分类Dev

如何在节点值的开头使用Perl在XML中添加新的子节点

来自分类Dev

使用当前节点位置并在graphviz中添加新节点

来自分类Dev

在XML中添加其他节点

来自分类Dev

C#在xml中添加节点

来自分类Dev

通过Augeas向XML根节点添加属性的问题

来自分类Dev

将新节点添加到xml文件

来自分类Dev

将新的节点/列添加到xml / xslt

来自分类Dev

向XML :: LibXML对象添加新的节点/元素

来自分类Dev

使用Augeas将新节点添加到XML文件

来自分类Dev

使用XML:libXML抛出错误添加新节点

来自分类Dev

如何将新节点添加到xml文件

来自分类Dev

将新节点链接到c中的链接列表的问题

来自分类Dev

将新节点链接到c中的链接列表的问题

来自分类Dev

使用R修改XML节点顺序(或将新节点添加到特定位置)

来自分类Dev

使用R修改XML节点顺序(或将新节点添加到特定位置)

来自分类Dev

使用 DOMDocument 将新节点及其子节点添加到 XML 文档

来自分类Dev

使用xml在python中添加完整的xml作为xml节点的子节点

来自分类Dev

添加或更新XML节点

来自分类Dev

在C ++的链表中添加新节点时程序崩溃

来自分类Dev

在C中的链表的末尾添加一个新节点

来自分类Dev

如何在AgensGraph中向节点添加新属性?

来自分类Dev

如何在树角中添加新节点?

来自分类Dev

在现有XML文档中追加新节点

来自分类Dev

新节点引导程序的问题

来自分类Dev

在根节点之前的xml文件中添加换行符

来自分类Dev

如何使用C#在XML中添加父节点

Related 相关文章

热门标签

归档