score:1
Accepted answer
Select page which you need
XDocument xdoc = XDocument.Load("Test.xml");
int pageId = 2;
var page = xdoc.Descendants("Page")
.FirstOrDefault(p => (int)p.Element("Id") == pageId);
And then add content to this page element (if any):
if (page != null)
{
// add to Hotspots element
page.Element("Hotspots")
.Add(new XElement("Hotspot",
new XElement("X", x),
new XElement("Y", y),
new XElement("Shape", "Circle"),
new XElement("TargetId", nNodeID)));
xdoc.Save("Test.xml");
}
Your code adds new Hotspot element to page, instead of adding content to existing Hotspots element.
Source: stackoverflow.com
Related Articles
- Editing XML document using C#
- Convert string[] to int[] in one line of code using LINQ
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Entity-framework code is slow when using Include() many times
- How to properly search xml document using LINQ C#
- Get and Add/Update multilevel embedded/nested MongoDB document using C#
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- can my code improve from using LINQ?
- LINQ Source Code Available
- Sub-Query or Join with embedded/nested document using C# LINQ with MongoDB
- .NET 4 Code Contracts: "requires unproven: source != null"
- How can I write the following code more elegantly using LINQ query syntax?
- How can I code an outer join using LINQ and EF6?
- Explanation of Code : retrieve item from Array using FirstorDefault()
- Create a new XMLDocument by filtering an existing document in c# using xpath
- C# .Net 3.5 Code to replace a file extension using LINQ
- Entity Framework Code First using context in Controller
- Trying to understand LINQ code using c#
- Retrieve bool result by using LinQ code
- Token EndDocument in state Document would result in an invalid XML document when using .Save()
- How to get the exact child element with corresponding parent only from multilevel embedded MongoDB document using C#
- Unit testing code using IQueryable
- How can I combine IObservable<T>.Throttle() with some other event source using Reactive Extensions?
- creating Linq to sqlite dbml from DbLinq source code
- Simple Edit/Update actions using LINQ. Isn't my code a bit wrong?
- Mongodb C# FindAsync. Filter on list inside document using linq
- Error Reading RSS Feed using LINQ to XML
- Linq Query Help Needed
- A C# Linq to Sql query that uses SUM, Case When, Group by, outer join, aggregate and defaults
- How to place an inline conditional union/self join in lambda expression?
- LINQ: Customize the count method
- Evaluate a lambda expression
- LINQ not returning desired data
- LINQ Conditional Where Clauses not working
- LINQ to XML: How to get all elements by value
- SQL query to linq in mvc
- MVC Persist Collection ViewModel (Update, Delete, Insert)
- Create dynamic object hierarchy for Json serialization via Linq
- How do I build up LINQ dynamically
- Remove Item in Dictionary based on Value
- Json.net start from value in array
- How to build Expression<Func<T,bool>> from Expression<Func<T>>
- Linq: dynamic Where clause inside a nested subquery
- Remove all switched dictionary pairs
- LIKEs and ORs and stuff in Linq
- Search for attribute value in xaml file using linq in C#?