Modifica esistente contenuti XML in C #
-
26-09-2019 - |
Domanda
ho trovato alcuni esempi di questo argomento. Alcuni degli esempi gived un metodo per modificare attributo con SelectNodes()
o SelectSingleNode()
, e altri gived il metodo per modificare attributo con someElement.SetAttribute("attribute-name", "new value");
Ma ho ancora confuso che come costruire la relazione se ho usato solo un XpathNodeItterator it
?
assunto ho definito come sotto,
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.
}
}
Esiste un metodo come it.setAttribute(name, "newValue")
?
Soluzione
MSDN : "An oggetto XPathNavigator viene creato da una classe che implementa l'interfaccia IXPathNavigable come classi XPathDocument e XmlDocument. XPathNavigator oggetti creati dagli oggetti XPathDocument sono di sola lettura mentre XPathNavigator oggetti creati dagli oggetti XmlDocument possono essere modificati. un XPathNavigator di sola lettura dell'oggetto o lo stato modificabile viene determinato utilizzando la proprietà CanEdit della classe XPathNavigator. "
Quindi, prima di tutto è necessario utilizzare XmlDocument, non XPathDocument, se si desidera impostare un attributo.
Un esempio di come modificare i dati XML utilizzando un XPathNavigator utilizzando il metodo CreateNavigator un XmlDocument, viene mostrato qui .
Come vedrete dalla esempio, v'è un metodo di ImpostaValore sul vostro oggetto it.Current.
Ecco come si dovrebbe fare per il vostro codice, con alcune lievi modifiche:
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.");
}
}
Altri suggerimenti
ho scritto un metodo di estensione che fornisce un metodo SetAttribute
per qualsiasi 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();
}
}
}