-1

I have that xml document :

<?xml version="1.0" encoding="utf-8" ?>

<reminders>
  <reminder>
    <Title>Alarm1</Title>
    <Description>Desc1</Description>
    <Time>03/07/2012 10:11AM</Time>
    <snooze>1</snooze>
    <repeat>None</repeat>
  </reminder>
  <reminder>
    <Title>Alarm2</Title>
    <Description>Desc2</Description>
    <Time>03/07/2012 10:11AM</Time>
    <snooze>15</snooze>
    <repeat>Daily</repeat>
  </reminder>
</reminders>

And say i would like to create a full reminder child like :

  <reminder>
    <Title>NEW-Alarm</Title>
    <Description>New-Desc</Description>
    <Time>03/07/2012 10:11AM</Time>
    <snooze>15</snooze>
    <repeat>Daily</repeat>
  </reminder>

How can i do that in C# ?

And also i`d like to edit some child like from :

<Title>NEW-Alarm</Title>

to be

<Title>Modified-NEW-Alarm</Title>

I am fresh to XML and i really did my best , actually i am opening like 13 webpages for xml but none of them has what i really need, so i`ll truly appreciate your help.

R.Vector
  • 1,669
  • 9
  • 33
  • 41
  • 1
    -1 - "opening like 13 webpages" does not yet show any sample code that you tried and have problems with. – Alexei Levenkov Mar 07 '12 at 19:20
  • I tried lots of code samples and none worked. – R.Vector Mar 07 '12 at 19:21
  • Well then, you didn't open enough web pages. – Bernard Mar 07 '12 at 19:24
  • Normally it is interesting why down votes, but I'm really interested why +1. @R.Vector, "none worked" is hard to help with - it is extremely rare that samples will do **exactly** what you want. SO is to help with concrete problems - show one and effort to solve and you get concrete useful answers. – Alexei Levenkov Mar 07 '12 at 19:33

3 Answers3

4

I would take a look at using XDocument. You may want to search the web for examples of creating XML with it but this answer from the unstoppable Jon Skeet is a good place to start:

XML file creation using XDocument in C#

Hope that helps.

Also see these links:

http://www.codeproject.com/Articles/169598/Parse-XML-Documents-by-XMLDocument-and-XDocument

http://www.leghumped.com/blog/2009/06/30/c-xml-with-xdocuments/

http://forums.asp.net/t/1736899.aspx/1?Help+using+XDocument+in+LINQ+with+ASP+Net+C+

Community
  • 1
  • 1
Richard
  • 21,728
  • 13
  • 62
  • 101
1

You need to look into the XDocument as a way to open your XML Document and then take a look at the documentation for XElement to see how easy it is to build nodes.

Each documentation page has great samples.

t3rse
  • 10,024
  • 11
  • 57
  • 84
1

Load the doc with XDocument class

Add a element (edit PATH with your data) :

XElement newEl = new XElement(new XElement("reminder",
                                new XElement("Title", "NEW-Alarm"),
                                new XElement("Description", "New-Desc"),
                                new XElement("Time", "03/07/2012 10:11AM"),
                                new XElement("snooze", "15"),
                                new XElement("repeat", "Daily")));
                    doc.Root.Add(newEl);
                    doc.Save(PATH);

To change, we must first find the element (with LINQ) and then apply the SetValue method. http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.setvalue.aspx

Julien
  • 3,509
  • 20
  • 35
  • Thank you for your answer , but i got an error : System.NullReferenceException: Object reference not set to an instance of an object. on doc.Root.Add(newEl); – R.Vector Mar 07 '12 at 19:51
  • You must load your document. See XDocument.Load() method – Julien Mar 07 '12 at 20:06