0

I'm having a problem to check if the element with some attributes exists. I will explain.. I have the following code

XDocument xDocument = XDocument.Parse(member.getProperty(itemlist).Value.ToString());
xDocument.Root.Add(new XElement(
    "Item",
    new XAttribute("Text", texts),
    new XAttribute("Value", values)));
member.getProperty(itemlist).Value = xDocument.ToString();
member.Save();

Well all I need you guys to understand is that simple - I convert something into my XDocument (witch is a xml) and I simple add a new item on it with some attributes. But now there is my issue. I want to do this : If that item with that attributes exists DELETE , if not ADD . How can I do this test? Thanks anyway all.

0

1 Answer 1

2

Do you mean something like this :

var existingItem = 
        xDocument.Root
                 .Elements("Item")
                 .FirstOrDefault(o => 
                                      (string)o.Attribute("Text") == texts 
                                        && 
                                      (string)o.Attribute("Value") == values
                                 );
if(existingItem != null)
{
    //DELETE !!!
}
else
{
    //ADD !!!
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thats it man, this is the verification that i needed ! Thank you sir :D
@hard07 this is working 100% but on the add, he duplicates the item any idea why ?
At the moment, No idea. It should work just fine as far as I can see. Consider to open new question if you can't find the solution, post your codes for the ADD part there..

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.