0

Please note the very last comment: The OP just forgot to call Save().


I am checking XML for attribute ProductCount under ProductDetails node, and if the attribute is not present add the attribute with a default value under this node.

I am able to check if the attribute exists or not but I am not able to add it, though it does not give me any error but does not even add the attribiute.

here is my code:

 XDocument XMLDoc = XDocument.Load(fileName);

 foreach (var detail in XMLDoc.Descendants(_ns + "ProductDetails"))
 {
    if (detail.Attribute("ProductCount") == null)
    {
        detail.SetAttributeValue("ProductCount", "1");
    }
 }

_ns has my namespace.

I am not able to figure out what I am doing wrong, why is it not adding ProductCount attribute if it does not exist.

7
  • My suspect is that the "foreach" doesn't match any element. Could you post a fragment of the source document? Then, how is defined the _ns variable? Commented Jul 12, 2011 at 10:15
  • <productDetails hadSuspension="false" isComplimentary="false" hasFamily1="false" hasFamily2="false" isInsured="false" hasJoint="false"/> Commented Jul 12, 2011 at 10:19
  • Your basic code is OK, how do you establish that "it doesn't work"? Did you use a debugger? Commented Jul 12, 2011 at 10:20
  • foreach is fine I tested it I searched for the attribute that is in the XML and it was OK, if the attribute is not there it gives null, I even checked that the control goes inside the if statement but it does not add the attribute. Commented Jul 12, 2011 at 10:21
  • "productDetails" begins with lower case? In the code you used the upper "P". Always in the snippet you added a namespace: where is in the xml fragment? Are you sure that the enumeration yields some result? Commented Jul 12, 2011 at 10:25

2 Answers 2

4

Try to pass value as integer instead of string like this:

detail.SetAttributeValue("ProductCount", 1);

EDIT: wrote bad suggestion...but I tried to test your problem...

created a.xml file with content:

<root>
  <ProductDetails hadSuspension="false" isComplimentary="false" hasFamily1="false" hasFamily2="false" isInsured="false" hasJoint="false" ProductCount="1"/>
  <ProductDetails hadSuspension="false" isComplimentary="false" hasFamily1="false" hasFamily2="false" isInsured="false" hasJoint="false"/>
</root>

test code:

XDocument XMLDoc = XDocument.Load("C:\\a.xml");

foreach (var detail in XMLDoc.Descendants("ProductDetails"))
{
   //Dont need to check, because SetAttributeValue creates if not exists
   //if(detail.Attribute("ProductCount") == null)
   detail.SetAttributeValue("ProductCount", 2);
}
XMLDoc.Save("C:\\b.xml");

and result b.xml file content:

<root>
  <ProductDetails hadSuspension="false" isComplimentary="false" hasFamily1="false" hasFamily2="false" isInsured="false" hasJoint="false" ProductCount="2" />
  <ProductDetails hadSuspension="false" isComplimentary="false" hasFamily1="false" hasFamily2="false" isInsured="false" hasJoint="false" ProductCount="2" />
</root>

So everything is working for me.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

if (detail.Attribute("ProductCount") == null)
{
    detail.Add(new XAttribute("ProductCount", "1"));
}

3 Comments

I tried but no luck :-( Tried both the options below detail.Add(new XAttribute("ProductCount", "1")); detail.Add(new XAttribute(_ns + "ProductCount", "1"));
@rs If it is the namespace then you also have to use _ns + in the if() statement.
Thanks all for responses, I figured out I missed to Save the document.

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.