Question

J'ai trouvé quelques exemples sur ce sujet. Certains des exemples gived un procédé pour modifier l'attribut avec SelectNodes() ou SelectSingleNode(), et d'autres gived le procédé pour modifier l'attribut avec someElement.SetAttribute("attribute-name", "new value");

Mais je reste confus que la façon de construire la relation si je seulement XpathNodeItterator it?

ASSUMED I défini comme ci-dessous,

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. 
   }
}

Y at-il une méthode comme it.setAttribute(name, "newValue")?

Était-ce utile?

La solution

De MSDN : « Un objet XPathNavigator est créé à partir d'une classe qui implémente l'interface IXPathNavigable tels que les classes XPathDocument et XmlDocument. XPathNavigator objets créés par des objets XPathDocument sont en lecture seule tandis que les objets XPathNavigator créés par des objets XmlDocument peut être édité. un XPathNavigator la lecture seule de l'objet ou de l'état modifiable est déterminée en utilisant la propriété CanEdit de la classe XPathNavigator. "

Alors, tout d'abord, vous devez utiliser XmlDocument, pas XPathDocument, si vous souhaitez définir un attribut.

Un exemple de la façon de modifier des données XML à l'aide d'un XPathNavigator en utilisant la méthode CreateNavigator d'un XmlDocument, est représenté ici .

Comme vous le verrez dans l'exemple, il existe une méthode SetValue sur votre objet it.Current.

Voici comment vous le feriez pour votre code, avec quelques légères modifications:

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

Autres conseils

J'ai écrit une méthode d'extension qui fournit une méthode de SetAttribute pour tout 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();
        }
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top