I Want to add the data to the xml file from my asp.net GUI.So i have a textbox in GUI.
So if user enters “IL” then i want to add a section in this way
<Employee Location="IL">
<Male Value="True" />
<Name Value="xxx" />
</Employee>
XML file:
<Emp>
<Employee Location="NJ">
<Male Value="True" />
<Name Value="xxx" />
</Employee>
<Employee Location="NY">
<Male Value="True" />
<Name Value="xxx" />
</Employee>
</Emp>
Note:
Every time i add a new section here the inner elements are constant i.e. the following values are always to be same.
<Male Value="True" />
<Name Value="xxx" />
I am looking for how can i achieve this using LINQ to XML?
Since only variable part of node you’re adding is
Locationattribute, you can very easily extract that process to a method, like the following one:Now when you want to update your existing XML with new employee data, you do it this way:
New employee node will be added to existing ones.
For more information on XML trees creation with LINQ to XML (as this is what we’re dealing with here), check online guide here.