-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.

4
  • 1
    -1 - "opening like 13 webpages" does not yet show any sample code that you tried and have problems with. Commented Mar 7, 2012 at 19:20
  • I tried lots of code samples and none worked. Commented Mar 7, 2012 at 19:21
  • Well then, you didn't open enough web pages. Commented Mar 7, 2012 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. Commented Mar 7, 2012 at 19:33

3 Answers 3

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+

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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);
You must load your document. See XDocument.Load() method
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.

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.