Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1054387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:23:56+00:00 2026-05-16T17:23:56+00:00

I found some examples about this subject. Some of the examples gived a method

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:23:57+00:00Added an answer on May 16, 2026 at 5:23 pm

    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.");
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found some info on the subject ( like this link) , but
After googling a bit I have found some tips about how to get online
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
I found some code in a project which looks like that : int main(int
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I have found some libraries or web services in PHP that does the job.
Quick question regarding CSS and the browser. I tried searching SO and found some
I've been parsing through some log files and I've found that some of the
I'm currently working on project with Haskell, and have found myself some trouble. I'm
I've found that on some occasions I can edit the source while debugging. Are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.