views:

49

answers:

1

Hello

I found some examples about this subject. Some of the examples gived a method to modify attribute with SelectNodes() or SelectSingleNode(), and others gived the method to modify attribute with someElement.SetAttribute("attribute-name", "new value");

But I still confused that how to build the relation if I only used a XpathNodeItterator it?

Assumed I defined as below,

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

Is there a method like it.setAttribute(name, "newValue") ?

+1  A: 

From MSDN: "An XPathNavigator object is created from a class that implements the IXPathNavigable interface such as the XPathDocument and XmlDocument classes. XPathNavigator objects created by XPathDocument objects are read-only while XPathNavigator objects created by XmlDocument objects can be edited. An XPathNavigator object's read-only or editable status is determined using the CanEdit property of the XPathNavigator class."

So, first of all you have to use XmlDocument, not XPathDocument, if you want to set an attribute.

An example of how to modify XML data using an XPathNavigator using the CreateNavigator method of an XmlDocument, is shown here.

As you'll see from the example, there is a method SetValue on your it.Current object.

Here's how you would do it for your code, with some slight 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.");
            }
        }
Richard Hein
@Richard Hein, Thanks for your input. I studied the MSDN tutorial. I found the method of 'SetValue' was used to update element value, not attribute value. Can tried it but failed. I need to find a way to update attribute value.
Nano HE
@Richard Hein, I found your detail update now. Introduced `it.MoveToAttribute()`. I will try it now. Thank you.
Nano HE
@Richard Hein, There is no `var` in .NET 2.0. I can't use `int.Parse(name)` when I pass a string type value to `name`. Throw out **Input string was not in a correct format.**. How can I fix it?
Nano HE
In your code sample, your int was coming from the vid attribute, which I missed in my sample. I've updated my code to reflect that.
Richard Hein