簡體   English   中英

在XML中添加新節點的問題

[英]Issue with adding a new node in XML

我試圖在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

我在這里做錯了什么?

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");

我不確定您是否打算將其設置為名稱空間,因為它是Schema文件,所以也許您是這個意思?:

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),並且已經添加到element上。 參見http://www.w3schools.com/schema/el_sequence.asp

maxOccurs可選。 指定序列元素可以在父元素中出現的最大次數。 該值可以是大於等於0的任何數字,或者如果您不希望限制最大數目,請使用值“ unbounded”。 預設值為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/zh-cn/library/x6c1kb0s(v=vs.110).aspx並使用XMLSerializer-> http:// msdn .microsoft.com / de-de / library / system.xml.serialization.xmlserializer(v = vs.110).aspx

您為什么不通過使用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");
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM