1

I want to add attributes only when it is not exist.

3 Answers 3

2

Check out: http://egeveke.blogspot.com/2006/09/xpathnavigator-missing-setattribute.html

Basically you use MoveToAttribute, testing if it was successful. If not, then it does not exist and you can create. Remember to either start with a clone of you navigator, or move back to the parent when you are successful.

if (nav.MoveToAttribute())
{
  // exists
  nav.MoveToParent();
} 
else
{
  nav.CreateAttribute(...);
} 
Sign up to request clarification or add additional context in comments.

Comments

0

Check out Insert XML Data using XPathNavigator. Specifically the section Inserting Attribute Nodes

1 Comment

but it doesn't check attribute exist or not
0

Use "HasAttributes" property on the Xpathnavigator object to check whether it has any attributes and then continue to do your operation. Hope this helps!!

3 Comments

I want to check for a particular attribute??/
Try using GetAttribute method and based on its return value, u can add the new attribute, if it does not exist.
you can also try using MoveToFirstAttribute() and then get its name and value like: ` if (xPathNav.MoveToFirstAttribute()) { Console.WriteLine(xPathNav.Name + "=" + xPathNav.Value); }`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.