0

This is my XML file i want to add new "proj" element and a attribute to that bases on the condition ,that project name is already there or not:

<?xml version="1.0" encoding="utf-8"?>
<projects>
  <proj name="project1">
    <file_type Type="internalmeeting">"path1"</file_type>
  </proj>
  <proj name="project2">
    <file_type Type="externalmeeting">"path2"</file_type>
  </proj>

</projects>

i m able to add the element but it adding each time i click "save" button.

6
  • Well have you looked at any of the XML APIs available in C#? I'd suggest that you read up on LINQ to XML. Start here: msdn.microsoft.com/en-us/library/bb387061.aspx Commented Mar 4, 2014 at 14:46
  • yes jon i did that ,actually my problem is to check the condition. Commented Mar 4, 2014 at 14:53
  • Check what condition? You've said about "the condition" but not explained what condition you're talking about. Please read tinyurl.com/so-hints Commented Mar 4, 2014 at 15:01
  • sorry for inconvenient question.i have a textbox in a form when user enters the project name code should check existing nodes and should decide to add or not to add. Commented Mar 5, 2014 at 4:58
  • Okay, so does the answer you've received satisfy that? (It looks fine to me.) Commented Mar 5, 2014 at 6:42

1 Answer 1

2

You can check whether the project exists with the given name like this before you add a new element:

var doc = XDocument.Load(path);
if(!doc.Descendants("proj").Any(x => (string)x.Attribute("name") == projectName))
{
    // add new project
}
Sign up to request clarification or add additional context in comments.

Comments

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.