1

私はこの主題についていくつかの例を見つけました。いくつかの例では、SelectNodes()またはSelectSingleNode()で属性を変更するメソッドを提供し、他の例では、またはで属性を変更するメソッドを提供しました。someElement.SetAttribute("attribute-name", "new value");

しかし、私がXpathNodeItterator it?だけを使用した場合、関係を構築する方法についてはまだ混乱しています。

私が以下のように定義したと仮定すると、

System.Xml.XPath.XPathDocument doc = new XPathDocument(xmlFile);
System.Xml.XPath.XPathNavigator nav = doc.CreateNavigator();
System.Xml.XPath.XPathNodeIterator it;

it = nav.Select("/Equipment/Items/SubItmes");
while (it.MoveNext())
{
   name = it.Current.GetAttribute("name ", it.Current.NamespaceURI);
   int vidFromXML = int.Parse(it.Current.GetAttribute("vid", it.Current.NamespaceURI));
   if (vidFromXML = vid)
   { 
    // How can I find the relation between it and element and node? I want to modify name attribute value. 
   }
}

のような方法はありit.setAttribute(name, "newValue")ますか?

4

2 に答える 2

4

MSDNから:「XPathNavigatorオブジェクトは、XPathDocumentクラスやXmlDocumentクラスなどのIXPathNavigableインターフェイスを実装するクラスから作成されます。XPathDocumentオブジェクトによって作成されたXPathNavigatorオブジェクトは読み取り専用ですが、XmlDocumentオブジェクトによって作成されたXPathNavigatorオブジェクトは編集できます。XPathNavigatorオブジェクトの読み取り-ステータスのみまたは編集可能ステータスは、XPathNavigatorクラスのCanEditプロパティを使用して決定されます。」

したがって、属性を設定する場合は、まずXPathDocumentではなくXmlDocumentを使用する必要があります。

XmlDocumentのCreateNavigatorメソッドを使用してXPathNavigatorを使用してXMLデータを変更する方法の例をここに示します

例からわかるように、it.CurrentオブジェクトにはメソッドSetValueがあります。

若干の変更を加えて、コードに対してこれを行う方法は次のとおりです。

        int vid = 2;
        var doc = new XmlDocument();
        doc.LoadXml("<Equipment><Items><SubItems  vid=\"1\" name=\"Foo\"/><SubItems vid=\"2\" name=\"Bar\"/></Items></Equipment>");
        var nav = doc.CreateNavigator();

        foreach (XPathNavigator it in nav.Select("/Equipment/Items/SubItems"))
        {
            if(it.MoveToAttribute("vid", it.NamespaceURI)) {
                int vidFromXML = int.Parse(it.Value);                    
                if (vidFromXML == vid)
                {
                    // if(it.MoveToNextAttribute() ... or be more explicit like the following:

                    if (it.MoveToParent() && it.MoveToAttribute("name", it.NamespaceURI))
                    {
                        it.SetValue("Two");
                    } else {
                        throw new XmlException("The name attribute was not found.");
                    }                
                }
            } else {
                    throw new XmlException("The vid attribute was not found.");
            }
        }
于 2010-09-02T01:46:36.080 に答える
0

SetAttribute私は任意のメソッドを提供する拡張メソッドを作成しましたXPathNavigator

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace My.Shared.Utilities {
    public static class XmlExtensions {
        public static void SetAttribute(this XPathNavigator nav, string localName, string namespaceURI, string value) {
            if (!nav.MoveToAttribute(localName, namespaceURI)) {
                throw new XmlException("Couldn't find attribute '" + localName + "'.");
            }
            nav.SetValue(value);
            nav.MoveToParent();
        }
    }
}
于 2013-01-29T10:58:33.110 に答える