-
April 19th, 2004, 12:22 PM
#1
ASP.NET / XML problem (Writing to an XML file)
I'm not sure where I should be posting this, if it's more of an ASP problem or an XML problem, so I apologize if it's in the wrong location.
I'm brand new to using XML and ASP (.NET or otherwise) (in fact this is my first project), so bear with me, I may need a little hand-holding.
I'm trying to create a page which simply adds data to an existing xml file. Ultimately it will add the data from a form, but to keep things simple, I've been simply using pre-defined values.
Originally I was able to get the script to write the xml exactly as I needed, however it would simply overwrite the existing file every time it was accessed. This is unacceptable, as it will be used to add data to one file, sort of like a mini database. So after much pain and gnashing of teeth I managed to get the script to append the data to an existing file. However, it wrote the xml wrong! ...I still cannot get it to format the xml properly, and am at my wits end as to how I should fix it.
If anyone could help me with this, I would be very appreciative!
Thanks!
This is how I want my xml to look:
PHP Code:
<?xml version="1.0"?>
<leads>
<lead>
<name>mac</name>
<phone>555-123-4567</phone>
<email>mac@daddy.com</email>
<besttimetocall>3pm</besttimetocall>
<wheretravel>iceland</wheretravel>
<ticketsneeded>4</ticketsneeded>
<numbertimestravel>3</numbertimestravel>
</lead>
<lead>
<name>mr jones</name>
<phone>555-555-5555</phone>
<email>mrjones@wherever.com</email>
<besttimetocall>10am</besttimetocall>
<wheretravel>bermuda</wheretravel>
<ticketsneeded>2</ticketsneeded>
<numbertimestravel>1</numbertimestravel>
</lead>
</leads>
Unfortunately, when I write to the file with my xmlwriter program, it ends up looking like this:
PHP Code:
<?xml version="1.0"?>
<leads>
<lead />
<name>moo</name>
<phone>324982</phone>
<email>moo@meh.com</email>
<besttimetocall>never</besttimetocall>
<wheretravel>test</wheretravel>
<ticketsneeded>test</ticketsneeded>
<numbertimestravel>test</numbertimestravel>
<lead />
<name>bubba</name>
<phone>555-555-5555</phone>
<email>bubba@nowhere.com</email>
<besttimetocall>3pm</besttimetocall>
<wheretravel>bermuda!</wheretravel>
<ticketsneeded>2</ticketsneeded>
<numbertimestravel>1</numbertimestravel>
<lead />
<name>bubba</name>
<phone>555-555-5555</phone>
<email>bubba@nowhere.com</email>
<besttimetocall>3pm</besttimetocall>
<wheretravel>bermuda!</wheretravel>
<ticketsneeded>2</ticketsneeded>
<numbertimestravel>1</numbertimestravel>
</leads>
If you'll notice, the tags are not falling into the <lead> </lead> tags like I want them to. Below follows the script that writes the data:
PHP Code:
<%@ Page Language="VB" debug="true" %>
<%@ Import Namespace="System.Xml" %>
<script runat=server>
sub Page_Load(obj as object, e as eventargs)
dim xmldoc as new XMLDocument()
dim strOutput as string = ""
try
xmldoc.Load(Server.MapPath("leads2.xml"))
dim eleLead as XmlElement = xmldoc.CreateElement ("lead")
dim eleName as XmlElement = xmldoc.CreateElement ("name")
dim eleNameText as XmlText = xmldoc.CreateTextNode("bubba")
dim elePhone as XmlElement = xmldoc.CreateElement ("phone")
dim elePhoneText as XmlText = xmldoc.CreateTextNode("555-555-5555")
dim eleEmail as XmlElement = xmldoc.CreateElement ("email")
dim eleEmailText as XmlText = xmldoc.CreateTextNode("bubba@nowhere.com")
dim eleBestTimeToCall as XmlElement = xmldoc.CreateElement ("besttimetocall")
dim eleBestTimeToCallText as XmlText = xmldoc.CreateTextNode("3pm")
dim eleWhereTravel as XmlElement = xmldoc.CreateElement ("wheretravel")
dim eleWhereTravelText as XmlText = xmldoc.CreateTextNode("bermuda!")
dim eleTicketsNeeded as XmlElement = xmldoc.CreateElement ("ticketsneeded")
dim eleTicketsNeededText as XmlText = xmldoc.CreateTextNode("2")
dim eleNumberTimesTravel as XmlElement = xmldoc.CreateElement ("numbertimestravel")
dim eleNumberTimesTravelText as XmlText = xmldoc.CreateTextNode("1")
xmldoc.DocumentElement.AppendChild(eleLead)
xmldoc.DocumentElement.AppendChild(eleName)
xmldoc.DocumentElement.LastChild.AppendChild(eleNameText)
xmldoc.DocumentElement.AppendChild(elePhone)
xmldoc.DocumentElement.LastChild.AppendChild(elePhoneText)
xmldoc.DocumentElement.AppendChild(eleEmail)
xmldoc.DocumentElement.LastChild.AppendChild(eleEmailText)
xmldoc.DocumentElement.AppendChild(eleBestTimeToCall)
xmldoc.DocumentElement.LastChild.AppendChild(eleBestTimeToCallText)
xmldoc.DocumentElement.AppendChild(eleWhereTravel)
xmldoc.DocumentElement.LastChild.AppendChild(eleWhereTravelText)
xmldoc.DocumentElement.AppendChild(eleTicketsNeeded)
xmldoc.DocumentElement.LastChild.AppendChild(eleTicketsNeededText)
xmldoc.DocumentElement.AppendChild(eleNumberTimesTravel)
xmldoc.DocumentElement.LastChild.AppendChild(eleNumberTimesTravelText)
xmldoc.Save(Server.MapPath("leads2.xml"))
catch ex as Exception
strOutput = "Error accessing XML file"
end try
output.Text = "Append operation successful"
End Sub
</script>
<html><body>
<asp:Label id="output" runat="server" />
</body>
</html>
Any idea how I get the information to properly fall inside the <lead> </lead> tags?
Thanks!
Last edited by dotNetDave; April 19th, 2004 at 12:24 PM.
-
April 19th, 2004, 09:41 PM
#2
You should make your code a bit clean. I modified your code and it should work !
PHP Code:
<%@ Page Language="VB" debug="true" %>
<%@ Import Namespace="System.Xml" %>
<script runat=server>
sub Page_Load(obj as object, e as eventargs)
dim xmldoc as new XMLDocument()
dim strOutput as string = ""
try
// Load XML File
xmldoc.Load(Server.MapPath("leads2.xml"))
// Create the required nodes !
dim eleLead as XmlElement = xmldoc.CreateElement("lead")
dim eleName as XmlElement = xmldoc.CreateElement("name")
dim elePhone as XmlElement = xmldoc.CreateElement("phone")
dim eleEmail as XmlElement = xmldoc.CreateElement("email")
dim eleBestTimeToCall as XmlElement = xmldoc.CreateElement("besttimetocall")
dim eleWhereTravel as XmlElement = xmldoc.CreateElement("wheretravel")
dim eleTicketsNeeded as XmlElement = xmldoc.CreateElement("ticketsneeded")
dim eleNumberTimesTravel as XmlElement = xmldoc.CreateElement("numbertimestravel")
// Set the text
dim eleNameText as XmlText = xmldoc.CreateTextNode("bubba")
dim elePhoneText as XmlText = xmldoc.CreateTextNode("555-555-5555")
dim eleEmailText as XmlText = xmldoc.CreateTextNode("bubba@nowhere.com")
dim eleBestTimeToCallText as XmlText = xmldoc.CreateTextNode("3pm")
dim eleWhereTravelText as XmlText = xmldoc.CreateTextNode("bermuda!")
dim eleTicketsNeededText as XmlText = xmldoc.CreateTextNode("2")
dim eleNumberTimesTravelText as XmlText = xmldoc.CreateTextNode("1")
// Append parentnode to root
xmldoc.DocumentElement.PrependChild(eleLead);
// append the nodes to the parentNode without the value
eleLead.AppendChild(eleName)
eleLead.AppendChild(elePhone)
eleLead.AppendChild(eleEmail)
eleLead.AppendChild(eleBestTimeToCall)
eleLead.AppendChild(eleWhereTravel)
eleLead.AppendChild(eleTicketsNeeded)
eleLead.AppendChild(eleNumberTimesTravel)
// Save the text values to the nodes
eleName.AppendChild(eleNameText)
elePhone.AppendChild(elePhoneText)
eleEmail.AppendChild(eleEmailText)
eleBestTimeToCall.AppendChild(eleBestTimeToCallText)
eleWhereTravel.LastChild.AppendChild(eleWhereTravelText)
eleTicketsNeeded.AppendChild(eleTicketsNeededText)
eleNumberTimesTravel.AppendChild(eleNumberTimesTravelText)
// Store the file
xmldoc.Save(Server.MapPath("leads2.xml"))
catch ex as Exception
strOutput = "Error accessing XML file"
end try
output.Text = "Append operation successful"
End Sub
</script>
aonu
-
April 28th, 2004, 04:52 PM
#3
Thank you very much Sonu! It worked beautifully! I really appreciate the help!
-
April 28th, 2004, 06:38 PM
#4
No prob! Nice to see some positive feedback.
Sonu
-
May 13th, 2004, 02:07 PM
#5
Hey Sonu,
Can u also show me the code for appending data to an xml file for C++ .NET. Just like in this example but in C++ because I am doing this project which requires me to store data from a data grid or a passed on xml file. Thanks.
-
May 13th, 2004, 02:09 PM
#6
Sorry, but I have never done this in C++. I suggest you to take a look at the functions I use and search the MSDN for that. Usually each of the functions contains an example in VB.NET, C# and C++.
Sonu
-
May 14th, 2004, 02:06 AM
#7
Hi,
How do i append to existing XML file using VB?
i'm having probs about the streamwriter stuff...
is it possible to convert streamwriter to stream...
-
May 14th, 2004, 06:33 AM
#8
The above example is in VB.NET so it should work.
Sonu
-
December 2nd, 2005, 10:08 AM
#9
Re: ASP.NET / XML problem (Writing to an XML file)
Excellent work Sonu .... okay how do I get the xml to write to a certain line
What I'm looking for is for the script to go to the 12th line of the xml file and then push everything down one line and then write the xml nodes an information .... I am looking for this because I am making a page for non-programmers to easily update the rss feeds I am putting out in an administration section of the website... Can anyone help me on this issue?
The code above puts it in right after the <rss version="2.0"> tag but before the channel tag so it actually messes up my rss feed
-
December 2nd, 2005, 11:56 AM
#10
Re: ASP.NET / XML problem (Writing to an XML file)
Did you take a look at AppendChild? I think that you should load the xml into an XMLDocument and call AppendChild on the parent node where you need to add your new item.
-
December 2nd, 2005, 12:02 PM
#11
Re: ASP.NET / XML problem (Writing to an XML file)
Originally Posted by Sonu Kapoor
You should make your code a bit clean. I modified your code and it should work !
PHP Code:
<%@ Page Language="VB" debug="true" %>
<%@ Import Namespace="System.Xml" %>
<script runat=server>
sub Page_Load(obj as object, e as eventargs)
dim xmldoc as new XMLDocument()
dim strOutput as string = ""
try
// Load XML File
xmldoc.Load(Server.MapPath("leads2.xml"))
// Create the required nodes !
dim eleLead as XmlElement = xmldoc.CreateElement("lead")
dim eleName as XmlElement = xmldoc.CreateElement("name")
dim elePhone as XmlElement = xmldoc.CreateElement("phone")
dim eleEmail as XmlElement = xmldoc.CreateElement("email")
dim eleBestTimeToCall as XmlElement = xmldoc.CreateElement("besttimetocall")
dim eleWhereTravel as XmlElement = xmldoc.CreateElement("wheretravel")
dim eleTicketsNeeded as XmlElement = xmldoc.CreateElement("ticketsneeded")
dim eleNumberTimesTravel as XmlElement = xmldoc.CreateElement("numbertimestravel")
// Set the text
dim eleNameText as XmlText = xmldoc.CreateTextNode("bubba")
dim elePhoneText as XmlText = xmldoc.CreateTextNode("555-555-5555")
dim eleEmailText as XmlText = xmldoc.CreateTextNode("bubba@nowhere.com")
dim eleBestTimeToCallText as XmlText = xmldoc.CreateTextNode("3pm")
dim eleWhereTravelText as XmlText = xmldoc.CreateTextNode("bermuda!")
dim eleTicketsNeededText as XmlText = xmldoc.CreateTextNode("2")
dim eleNumberTimesTravelText as XmlText = xmldoc.CreateTextNode("1")
// Append parentnode to root
xmldoc.DocumentElement.PrependChild(eleLead);
// append the nodes to the parentNode without the value
eleLead.AppendChild(eleName)
eleLead.AppendChild(elePhone)
eleLead.AppendChild(eleEmail)
eleLead.AppendChild(eleBestTimeToCall)
eleLead.AppendChild(eleWhereTravel)
eleLead.AppendChild(eleTicketsNeeded)
eleLead.AppendChild(eleNumberTimesTravel)
// Save the text values to the nodes
eleName.AppendChild(eleNameText)
elePhone.AppendChild(elePhoneText)
eleEmail.AppendChild(eleEmailText)
eleBestTimeToCall.AppendChild(eleBestTimeToCallText)
eleWhereTravel.LastChild.AppendChild(eleWhereTravelText)
eleTicketsNeeded.AppendChild(eleTicketsNeededText)
eleNumberTimesTravel.AppendChild(eleNumberTimesTravelText)
// Store the file
xmldoc.Save(Server.MapPath("leads2.xml"))
catch ex as Exception
strOutput = "Error accessing XML file"
end try
output.Text = "Append operation successful"
End Sub
</script>
aonu
How would I do that?? I am using the script from this reply you had made I just changed the elements to what I need and I grab the info I need put into the elements from textboxes on the page before.
David Downey
Programmer/Web Developer/Point of Sales
Graphic Artist/Administrator
-
December 2nd, 2005, 12:18 PM
#12
Re: ASP.NET / XML problem (Writing to an XML file)
You need to do the following:
' take a look at: http://www.w3schools.com/xpath/xpath_syntax.asp for the correct XPath syntax.
Dim xmlNode as XmlNode = xmlDoc.SelectSingleNode("/rss/channel/item[link='YOUR_LINK'])
' Once you get the node
xmlNode.AppendChild(YourNewNode)
Hope it helps a bit.
-
December 2nd, 2005, 12:40 PM
#13
Re: ASP.NET / XML problem (Writing to an XML file)
well that brought me a little closer but not quite ... I think I might just use a DB instead and then just keep recreating the xml file from the DB but I'd rather learn how to directly write it... guess I just need to keep reading .... Thank you for your help though if you ever need anything I will try to help the same but I'm more into DB's and just strating to get into the xml
David Downey
Programmer/Web Developer/Point of Sales
Graphic Artist/Administrator
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|