Hi guys, I have a small problem
I am writing a program which adds informations about broken systems in my company.
Customers are informed about new events (outages) via RSS (XML) channels.

My program simply download xml from the web page and give a possibility to add new items (events) to the existing file, and then upload it back to the server...

My xml file looks like that:

?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>Testowo Inc.</title>
    <link>www.wp.pl</link>
    <description>Testowa wersja XML dla RSS via Outlook</description>
    <copyright>me:-)</copyright>
    <ttl>5</ttl>
    <item>
      <title>WMS sobie padnie</title>
	<description>
		WMS
		Jakis sobie tam opis..
	</description>
      <link>http://www.jakis.workpoint.wms.html</link>
      <pubDate>2011-07-23T14:34:00+02:00</pubDate>
    </item>
    
  

    <item>
      <title>WCP sobie padnie</title>
	<description>
		WCP
		Jakis sobie tam opis..
	</description>
      <link>http://www.jakis.workpoint.wcp.html</link>
      <pubDate>2011-07-23T14:34:00+02:00</pubDate>
    </item>
    
  </channel>
</rss>

All I want is a code which will add me new item to this file...
new item with new description, new link and date...
so after save it should look like that:

?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>Testowo Inc.</title>
    <link>www.wp.pl</link>
    <description>Testowa wersja XML dla RSS via Outlook</description>
    <copyright>me:-)</copyright>
    <ttl>5</ttl>
    <item>
      <title>WMS sobie padnie</title>
	<description>
		WMS
		Jakis sobie tam opis..
	</description>
      <link>http://www.jakis.workpoint.wms.html</link>
      <pubDate>2011-07-23T14:34:00+02:00</pubDate>
    </item>
    
  

    <item>
      <title>WCP sobie padnie</title>
	<description>
		WCP
		Jakis sobie tam opis..
	</description>
      <link>http://www.jakis.workpoint.wcp.html</link>
      <pubDate>2011-07-23T14:34:00+02:00</pubDate>
    </item>



    <item>
      <title>NEW ENTRY</title>
	<description>
		NEW ENTRY
	</description>
      <link>NEW ENTRY</link>
      <pubDate>NEW DATE</pubDate>
    </item>
    
  </channel>
</rss>

My current code doesn't work and it looks like that:

private void load_xml(string xml)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Application.StartupPath + "\\RSS\\" + xml + ".xml");
            //XmlNode xNode = xDoc.CreateNode(XmlNodeType.Element, "add", "");
            xDoc.CreateElement("item");
            xDoc.CreateAttribute("item", "NEW ENTRY");
            xDoc.CreateElement("description");
            xDoc.CreateAttribute("description", "NEW ENTRY");
            xDoc.Save(Application.StartupPath + "\\RSS\\" + xml + ".xml");
        }

THX for help!

Even though you have created a new element on the document you need to add it to a node for it to be added to the document.

The following snippet should get you started.

private void load_xml(string xml)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Application.StartupPath + "\\RSS\\" + xml + ".xml");

            // find channel element to append to
            var channels = xDoc.GetElementsByTagName("channel", string.Empty);
            // if no channel tags then exit
            if (channels.Count==0)
                return;
            // create new item
            var newItem = xDoc.CreateElement("item");
            // add attributes (with value) 
            // Note: You do not have any attributes in your xml.
            // This is added to show you how to do it and what the effect is.
            newItem.SetAttribute("name", "NewItem");
            // create and add item sub tags
            var description = xDoc.CreateElement("description");
            description.InnerText = "NEW ITEM";
            newItem.AppendChild(description);
            // add new item to first channel
            channels[0].AppendChild(newItem);

            xDoc.Save(Application.StartupPath + "\\RSS\\" + xml + ".xml");
        }

Hmm I have modified my program and I think I am close to my target:

RSS:

<rss version="2.0">
	<channel>
 		<title>WCP Information channel</title>
 		<link>http://www.efukt.com</link>
		<description>WCP Informator</description>
	</channel>
</rss>

and my code C#

tring strFilename = Application.StartupPath + "\\RSS\\" + file;
                XmlDocument xmlDoc = new XmlDocument();

                if (File.Exists(strFilename))
                {
                    xmlDoc.Load(strFilename);

                    XmlElement elmXML = xmlDoc.CreateElement("item");
                    string strNewvideo = "<title>TEST</title>" +
                                         "<description>" + EngBox.Text + "</description>"
                                         +"<link>" + LinkBox.Text + "</link>";
                    elmXML.InnerXml = strNewvideo;
                    xmlDoc.DocumentElement.AppendChild(elmXML);
                    xmlDoc.Save(strFilename);
                }

but the result has one problem:

<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>WCP Information channel</title>
    <link>http://www.efukt.com</link>
    <description>WCP Informator</description>
  </channel>
  <item>
    <title>TEST</title>
    <description>Available</description>
    <link>adsdasads</link>
  </item>
</rss>

</channel> should be after before </rss> item...I dunno how to modify the code to move </channel> to the end of file.

like this:

?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>WCP Information channel</title>
    <link>http://www.efukt.com</link>
    <description>WCP Informator</description>
  <item>
    <title>TEST</title>
    <description>Available</description>
    <link>adsdasads</link>
  </item>
</channel>
</rss>

You are appending to the root node of the document (in this case the rss element).
You need to append to the channel element; which you must find first.
Check my earlier post to see one way to do this.

The objects in System.Xml.Link namespace are a little easier to user than the System.Xml namespace objects.
You will need to add a reference to System.Xml.Link in you project reference.

private void load_xml2(string xml)
        {
            var xDoc = XDocument.Load(Application.StartupPath + "\\RSS\\" + xml + ".xml");

            // find channel element to append to (exit if not found)
            var channel = xDoc.Descendants("channel").ElementAt(0);
            if (channel == null)
                return;
            // create new item with sub tags
            var newItem = new XElement("item", 
                new XElement("title","New Item Title"),
                new XElement("description", "NEW ITEM"),
                new XElement("link", "Link to item"));
            // add new item to first channel
            channel.Add(newItem);
            
            xDoc.Save(Application.StartupPath + "\\RSS\\" + xml + "2.xml");
        }

Thank You mate, thats exactly what I was looking for :)
I owe you a beer :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.