BreakHead
BreakHead

Reputation: 10672

Add XMl attributes if not exist using Xpath Navigator

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

Upvotes: 1

Views: 1625

Answers (3)

Graham Lower
Graham Lower

Reputation: 71

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

Upvotes: 2

Archana
Archana

Reputation: 99

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

Upvotes: 0

SwDevMan81
SwDevMan81

Reputation: 50018

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

Upvotes: 0

Related Questions