I'm having problems with how to add elements to my XML document, I want to add hotspot information in xml, where Id is correct (so where id = 2 add hotspot information) this is my current XML -
<Pages> <Page> <Id>1</Id> <Title>TEST</Title> <ContentUrl>Images\testimg.png</ContentUrl> <Hotspots> <Hotspot> <X>140</X> <Y>202</Y> <Shape>Circle</Shape> <TargetId>2</TargetId> </Hotspot> </Hotspots> <ParentId>0</ParentId> </Page> <Page> <Id>2</Id> <Title>TEST2</Title> <ContentUrl>Images\testimg2.jpg</ContentUrl> <Hotspots> </Hotspots> <ParentId>1</ParentId> </Page> </Pages>
I want the xml to be updated so that it shows something like this -
<Pages> <Page> <Id>1</Id> <Title>TEST</Title> <ContentUrl>Images\testimg.png</ContentUrl> <Hotspots> <Hotspot> <X>140</X> <Y>202</Y> <Shape>Circle</Shape> <TargetId>2</TargetId> </Hotspot> </Hotspots> <ParentId>0</ParentId> </Page> <Page> <Id>2</Id> <Title>TEST2</Title> <ContentUrl>Images\testimg2.jpg</ContentUrl> <Hotspots> <Hotspot> <X>140</X> <Y>202</Y> <Shape>Circle</Shape> <TargetId>2</TargetId> </Hotspot> </Hotspots> <ParentId>1</ParentId> </Page>
The code I still have is
XDocument Xdoc = XDocument.Load(@"Test.xml"); Xdoc.Root.Element("Pages").Elements("Page").Where(Page => Page.Value.Substring(0,Page.Value.IndexOf("-"))==CurrentPage.Id.ToString()) .FirstOrDefault() .Add(new XElement("Hotspot", new XElement("X", x), new XElement("Y", y), new XElement("Shape", "Circle"), new XElement("TargetId", nNodeID) )); Xdoc.Save(@"Test.xml");
(CurrentPage.Id is the identifier that I want to map to the XML document, where you can add Hotspot - Page.Value.IndexOf ("-") returns the page identifier in xml)
but it just adds the code at the bottom of the page, so I need to find a way to add it to the Hotspots XML section where the correct identifier is located.
Any help would be appreciated, and if there is a better way to do what I'm trying, let me know, I have never worked with XML documents in my code before and only recently started to learn C # (in the last month).
thanks.