I have thie following code and it adds data to the xml below it. However, as you can see it posts the record below the <channel> tags. How do I append inside those tags?
Dim XMLd2 As New XmlDocument
XMLd2.Load("D:\Hosting\6078893\html\feed\rss-feed.xml")
Dim xmlEl2 As XmlElement = XMLd2.CreateElement("item")
xmlEl2.InnerXml = "<title></title><pubDate></pubDate><link></link><description></description>"
xmlEl2.Item("title").InnerText = "GOAT"
xmlEl2.Item("pubDate").InnerText = Now()
xmlEl2.Item("link").InnerText = "http://www.xxx.com/blogview.aspx?blogno=666"
xmlEl2.Item("description").InnerText = "fish"
XMLd2.DocumentElement.AppendChild(xmlEl2)
XMLd2.Save("D:\Hosting\html\feed\rss-feed.xml")
XML...
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
</channel>
<title>xxx Blog</title>
<link>http://www.xxx.com</link>
<description>The new home of the EU Referendum blog</description>
<item>
<title>mypage title</title>
<pubDate>4/9/2012 9:30:36 AM</pubDate>
<link>http://www.xxx.com/blogview.aspx?blogno=82462</link>
<description>my post;</description>
</item>
</rss>
XMLd2.Load("D:\Hosting\6078893\html\feed\rss-feed.xml") Dim n AsXmlNode
= XMLd2("channel") Dim xmlEl2 AsXmlElement=XMLd2.CreateElement("item") ...... XMLd2.DocumentElement.AppendChild(xmlEl2) n.AppendChild(xmlEl2)
...save...
Have you read the book? - ASP.Net 3.5 CMS Development (now on Kindle)
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
what part is it saying is NULL, the "n" or the "xmlEl2"?
You will want to step through and see....
I'm guessing its the "n", meaning it didn't find "channel" in your XML document. It looks like "channel" is a child node already, so you will have to locate that node in the XML first... I was using a snippet I had where "channel" would have been a top level
item, not a child already.
What I mean is you may need to define "n" as XMLd2("rss").ChildNodes("channel") instead.
Have you read the book? - ASP.Net 3.5 CMS Development (now on Kindle)
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
Lets say your rss xml looks something as below before appending nodes to it
rss-feed.xml
<rss version="2.0">
<channel>
</channel>
</rss>
If you write below code and run it
XDocument xDoc = XDocument.Load(Server.MapPath("rss-feed.xml"));
XElement xItem = new XElement("item");
XElement xTitle = new XElement("title", "mypage");
XElement xPub = new XElement("pubDate", "4/9/2012 9:30:36 AM");
XElement xLink = new XElement("link", "http://www.xxx.com/blogview.aspx?blogno=82462");
XElement xDesc = new XElement("description", "my post;");
//Lets insert the nodes in reverse case
xItem.Add(xTitle);
//Add the child node in the Item node
xItem.Add(xPub);
xItem.Add(xLink);
xItem.Add(xDesc);
//Add the item Node in Channel node
xDoc.Root.Element("channel").Add(xItem);
string seeOutPutHere = xDoc.ToString();
xDoc.Save(Server.MapPath("rss-feed.xml"));
I see. It doesn't like me. Throws a shoe on the dim. " Message=Conversion from string "channel" to type 'Integer' is not valid."
Dim XMLd2 As New XmlDocument
XMLd2.Load("c:\rss-feed.xml")
Dim n As XmlNode = XMLd2("rss").ChildNodes("channel")
Dim xmlEl2 As XmlElement = XMLd2.CreateElement("item")
xmlEl2.InnerXml = "<title></title><pubDate></pubDate><link></link><description></description>"
xmlEl2.Item("title").InnerText = e.Command.Parameters("@title").Value
xmlEl2.Item("pubDate").InnerText = Now()
xmlEl2.Item("link").InnerText = "http://www.xxx.com/blogview.aspx?blogno=" + System.Convert.ToString(newblog)
xmlEl2.Item("description").InnerText = e.Command.Parameters("@post").Value
n.AppendChild(xmlEl2)
XMLd2.Save("c:\rss-feed.xml")
acidtrash
Member
124 Points
158 Posts
Appending in existing XML tags
Apr 09, 2012 05:25 PM|LINK
I have thie following code and it adds data to the xml below it. However, as you can see it posts the record below the <channel> tags. How do I append inside those tags?
Dim XMLd2 As New XmlDocument XMLd2.Load("D:\Hosting\6078893\html\feed\rss-feed.xml") Dim xmlEl2 As XmlElement = XMLd2.CreateElement("item") xmlEl2.InnerXml = "<title></title><pubDate></pubDate><link></link><description></description>" xmlEl2.Item("title").InnerText = "GOAT" xmlEl2.Item("pubDate").InnerText = Now() xmlEl2.Item("link").InnerText = "http://www.xxx.com/blogview.aspx?blogno=666" xmlEl2.Item("description").InnerText = "fish" XMLd2.DocumentElement.AppendChild(xmlEl2) XMLd2.Save("D:\Hosting\html\feed\rss-feed.xml")XML...
<?xml version="1.0" encoding="ISO-8859-1"?> <rss version="2.0"> <channel> </channel> <title>xxx Blog</title> <link>http://www.xxx.com</link> <description>The new home of the EU Referendum blog</description> <item> <title>mypage title</title> <pubDate>4/9/2012 9:30:36 AM</pubDate> <link>http://www.xxx.com/blogview.aspx?blogno=82462</link> <description>my post;</description> </item> </rss>Curt_C
All-Star
66017 Points
7639 Posts
Moderator
Re: Appending in existing XML tags
Apr 09, 2012 06:15 PM|LINK
XMLd2.Load("D:\Hosting\6078893\html\feed\rss-feed.xml")
Dim n As XmlNode = XMLd2("channel")
Dim xmlEl2 As XmlElement = XMLd2.CreateElement("item")
......
XMLd2.DocumentElement.AppendChild(xmlEl2)
n.AppendChild(xmlEl2)
...save...
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
acidtrash
Member
124 Points
158 Posts
Re: Appending in existing XML tags
Apr 09, 2012 06:35 PM|LINK
Thank you for that.
However it throws on n.AppendChild(xmlEl2) saying that n is a null reference.
Curt_C
All-Star
66017 Points
7639 Posts
Moderator
Re: Appending in existing XML tags
Apr 09, 2012 07:33 PM|LINK
what part is it saying is NULL, the "n" or the "xmlEl2"?
You will want to step through and see....
I'm guessing its the "n", meaning it didn't find "channel" in your XML document. It looks like "channel" is a child node already, so you will have to locate that node in the XML first... I was using a snippet I had where "channel" would have been a top level item, not a child already.
What I mean is you may need to define "n" as XMLd2("rss").ChildNodes("channel") instead.
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
acidtrash
Member
124 Points
158 Posts
Re: Appending in existing XML tags
Apr 09, 2012 07:43 PM|LINK
I tried
Dim n as XMLd2("rss").ChildNodes("channel")
but it underlined rss saying "Array bounds cannot appear in ype specifiers"
I really don't know what that means.
Curt_C
All-Star
66017 Points
7639 Posts
Moderator
Re: Appending in existing XML tags
Apr 10, 2012 12:19 PM|LINK
Dim n as XmlNode = XMLd2("rss").ChildNodes("channel")
you forgot the AS XMLNODE
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
kavita_khand...
Star
9767 Points
1930 Posts
Re: Appending in existing XML tags
Apr 10, 2012 12:38 PM|LINK
@acidtrash
Lets say your rss xml looks something as below before appending nodes to it
rss-feed.xml
<rss version="2.0">
<channel>
</channel>
</rss>
If you write below code and run it
XDocument xDoc = XDocument.Load(Server.MapPath("rss-feed.xml")); XElement xItem = new XElement("item"); XElement xTitle = new XElement("title", "mypage"); XElement xPub = new XElement("pubDate", "4/9/2012 9:30:36 AM"); XElement xLink = new XElement("link", "http://www.xxx.com/blogview.aspx?blogno=82462"); XElement xDesc = new XElement("description", "my post;"); //Lets insert the nodes in reverse case xItem.Add(xTitle); //Add the child node in the Item node xItem.Add(xPub); xItem.Add(xLink); xItem.Add(xDesc); //Add the item Node in Channel node xDoc.Root.Element("channel").Add(xItem); string seeOutPutHere = xDoc.ToString(); xDoc.Save(Server.MapPath("rss-feed.xml"));You will get output as
<rss version="2.0">
<channel>
<item>
<title>mypage</title>
<pubDate>4/9/2012 9:30:36 AM</pubDate>
<link>http://www.xxx.com/blogview.aspx?blogno=82462</link>
<description>my post;</description>
</item>
</channel>
</rss>
I would love to change the world, but they wont give me the source code.
acidtrash
Member
124 Points
158 Posts
Re: Appending in existing XML tags
Apr 10, 2012 12:58 PM|LINK
I see. It doesn't like me. Throws a shoe on the dim. " Message=Conversion from string "channel" to type 'Integer' is not valid."
Dim XMLd2 As New XmlDocument XMLd2.Load("c:\rss-feed.xml") Dim n As XmlNode = XMLd2("rss").ChildNodes("channel") Dim xmlEl2 As XmlElement = XMLd2.CreateElement("item") xmlEl2.InnerXml = "<title></title><pubDate></pubDate><link></link><description></description>" xmlEl2.Item("title").InnerText = e.Command.Parameters("@title").Value xmlEl2.Item("pubDate").InnerText = Now() xmlEl2.Item("link").InnerText = "http://www.xxx.com/blogview.aspx?blogno=" + System.Convert.ToString(newblog) xmlEl2.Item("description").InnerText = e.Command.Parameters("@post").Value n.AppendChild(xmlEl2) XMLd2.Save("c:\rss-feed.xml")acidtrash
Member
124 Points
158 Posts
Re: Appending in existing XML tags
Apr 10, 2012 01:09 PM|LINK
kavita, that's just the ticket. Worked first time. Thank you VERY much. That's a big weight off my mind! Brilliant.