Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > XML File - reading and writing

Reply
Thread Tools

XML File - reading and writing

 
 
maw
Guest
Posts: n/a
 
      06-12-2007
Hi, could somebody point me in the right direction for adding, removing
and modifying nodes in an xml file programatically using vb.net (.net
framework 2.0)? I have an xml file in the following format which I need
to be able to add and remove records from. (I can not change this xml
file format.)

<country name="United Kingdom">
<city name="City1">
<street name="15 Street Address">
<details>
<postcode>POS COD</postcode>
<province>County1</province>
<c>GB</c>
</details>
</street>
</city>
<city name="City2">
<street name="10 street address">
<details>
<postcode>PST COD</postcode>
<province>County</province>
<c>GB</c>
</details>
</street>
</city>
</country>


I am having problems adding a node without overwriting existing data.
For example I can determine if a Country already exists in the XML file
but when I add the City and the Street items to it using the following
code, it replaces existing city and street data in the xml file rather
than appending new elements.

Sub InsertAddress(ByVal doc As XmlDocument)
Dim addresses As XmlNode = doc.DocumentElement
Dim country As XmlElement

If country_exists() Then
'append
country = doc.SelectSingleNode("//country[@name='Country']")
Else
country = doc.CreateElement("country")
country.SetAttribute("name", "Spain")
End If

Dim city As XmlElement = doc.CreateElement("city")
city.SetAttribute("name", "City3")

Dim street As XmlElement = doc.CreateElement("street")
street.SetAttribute("name", "20 Street Address")

Dim details As XmlElement = doc.CreateElement("details")
Dim postcode As XmlElement = doc.CreateElement("postcode")
postcode.InnerText = "POS COD"

Dim province As XmlElement = doc.CreateElement("province")
province.InnerText = "County3"

Dim c As XmlElement = doc.CreateElement("c")
c.InnerText = "UK"

country.AppendChild(city)
city.AppendChild(street)
street.AppendChild(details)
details.AppendChild(postcode)
details.AppendChild(province)
details.AppendChild(c)
addresses.AppendChild(country)
End Sub


Thanks for any help you can give.
Mark.
 
Reply With Quote
 
 
 
 
RSH
Guest
Posts: n/a
 
      06-12-2007
You can read the XML into a dataset and allow the user to work with that,
saving the results back to XML. Loading and saving data from a dataset is
quite simple and quite effective. In my opinion it is much easier to do
this than work with the XML directly.

Public Overrides Function GetDataSet() As System.Data.DataSet

Dim fs As FileStream

Try

fs = New FileStream(MyBase.FilePath, FileMode.Open)

MyBase.Dataset.ReadXml(fs)

MyBase.Dataset.Tables(0).PrimaryKey = New DataColumn()
{MyBase.Dataset.Tables(0).Columns(MyBase.PrimaryKe y)}

Return MyBase.Dataset

Catch exc As Exception

WriteToErrorLog(exc)

Finally

fs.Flush()

fs.Close()

End Try

End Function

Public Overrides Function SaveDataset(ByVal oDs As Dataset) As Boolean

Dim fs As System.IO.StreamWriter

Try

oDs.WriteXml(MyBase.FilePath, XmlWriteMode.WriteSchema)

fs = New System.IO.StreamWriter(MyBase.FilePath)

oDs.WriteXml(fs, XmlWriteMode.WriteSchema)

Catch exc As Exception

WriteToErrorLog(exc)

Finally

fs.Flush()

fs.Close()

End Try

End Function

End Class

Hope that helps,
Ron

"maw" <(E-Mail Removed)> wrote in message
news:466e60e7$0$30330$(E-Mail Removed)...
> Hi, could somebody point me in the right direction for adding, removing
> and modifying nodes in an xml file programatically using vb.net (.net
> framework 2.0)? I have an xml file in the following format which I need to
> be able to add and remove records from. (I can not change this xml file
> format.)
>
> <country name="United Kingdom">
> <city name="City1">
> <street name="15 Street Address">
> <details>
> <postcode>POS COD</postcode>
> <province>County1</province>
> <c>GB</c>
> </details>
> </street>
> </city>
> <city name="City2">
> <street name="10 street address">
> <details>
> <postcode>PST COD</postcode>
> <province>County</province>
> <c>GB</c>
> </details>
> </street>
> </city>
> </country>
>
>
> I am having problems adding a node without overwriting existing data. For
> example I can determine if a Country already exists in the XML file but
> when I add the City and the Street items to it using the following code,
> it replaces existing city and street data in the xml file rather than
> appending new elements.
>
> Sub InsertAddress(ByVal doc As XmlDocument)
> Dim addresses As XmlNode = doc.DocumentElement
> Dim country As XmlElement
>
> If country_exists() Then
> 'append
> country = doc.SelectSingleNode("//country[@name='Country']")
> Else
> country = doc.CreateElement("country")
> country.SetAttribute("name", "Spain")
> End If
>
> Dim city As XmlElement = doc.CreateElement("city")
> city.SetAttribute("name", "City3")
>
> Dim street As XmlElement = doc.CreateElement("street")
> street.SetAttribute("name", "20 Street Address")
>
> Dim details As XmlElement = doc.CreateElement("details")
> Dim postcode As XmlElement = doc.CreateElement("postcode")
> postcode.InnerText = "POS COD"
>
> Dim province As XmlElement = doc.CreateElement("province")
> province.InnerText = "County3"
>
> Dim c As XmlElement = doc.CreateElement("c")
> c.InnerText = "UK"
>
> country.AppendChild(city)
> city.AppendChild(street)
> street.AppendChild(details)
> details.AppendChild(postcode)
> details.AppendChild(province)
> details.AppendChild(c)
> addresses.AppendChild(country)
> End Sub
>
>
> Thanks for any help you can give.
> Mark.



 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      06-12-2007
maw wrote:

> I am having problems adding a node without overwriting existing data.
> For example I can determine if a Country already exists in the XML file
> but when I add the City and the Street items to it using the following
> code, it replaces existing city and street data in the xml file rather
> than appending new elements.
>
> Sub InsertAddress(ByVal doc As XmlDocument)
> Dim addresses As XmlNode = doc.DocumentElement
> Dim country As XmlElement
>
> If country_exists() Then
> 'append
> country = doc.SelectSingleNode("//country[@name='Country']")
> Else
> country = doc.CreateElement("country")
> country.SetAttribute("name", "Spain")
> End If
>
> Dim city As XmlElement = doc.CreateElement("city")
> city.SetAttribute("name", "City3")
>
> Dim street As XmlElement = doc.CreateElement("street")
> street.SetAttribute("name", "20 Street Address")
>
> Dim details As XmlElement = doc.CreateElement("details")
> Dim postcode As XmlElement = doc.CreateElement("postcode")
> postcode.InnerText = "POS COD"
>
> Dim province As XmlElement = doc.CreateElement("province")
> province.InnerText = "County3"
>
> Dim c As XmlElement = doc.CreateElement("c")
> c.InnerText = "UK"
>
> country.AppendChild(city)
> city.AppendChild(street)
> street.AppendChild(details)
> details.AppendChild(postcode)
> details.AppendChild(province)
> details.AppendChild(c)
> addresses.AppendChild(country)
> End Sub


With that code that does a lot of AppendChild calls you are only adding
new nodes but you are certainly not able to overwrite stuff that way.
So that code should work fine.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
mark
Guest
Posts: n/a
 
      06-12-2007
Thanks RSH, can I still use this method with the XML file structure I
posted, i.e using attributes?

RSH wrote:
> You can read the XML into a dataset and allow the user to work with that,
> saving the results back to XML. Loading and saving data from a dataset is
> quite simple and quite effective. In my opinion it is much easier to do
> this than work with the XML directly.
>
> Public Overrides Function GetDataSet() As System.Data.DataSet
>
> Dim fs As FileStream
>
> Try
>
> fs = New FileStream(MyBase.FilePath, FileMode.Open)
>
> MyBase.Dataset.ReadXml(fs)
>
> MyBase.Dataset.Tables(0).PrimaryKey = New DataColumn()
> {MyBase.Dataset.Tables(0).Columns(MyBase.PrimaryKe y)}
>
> Return MyBase.Dataset
>
> Catch exc As Exception
>
> WriteToErrorLog(exc)
>
> Finally
>
> fs.Flush()
>
> fs.Close()
>
> End Try
>
> End Function
>
> Public Overrides Function SaveDataset(ByVal oDs As Dataset) As Boolean
>
> Dim fs As System.IO.StreamWriter
>
> Try
>
> oDs.WriteXml(MyBase.FilePath, XmlWriteMode.WriteSchema)
>
> fs = New System.IO.StreamWriter(MyBase.FilePath)
>
> oDs.WriteXml(fs, XmlWriteMode.WriteSchema)
>
> Catch exc As Exception
>
> WriteToErrorLog(exc)
>
> Finally
>
> fs.Flush()
>
> fs.Close()
>
> End Try
>
> End Function
>
> End Class
>
> Hope that helps,
> Ron
>
> "maw" <(E-Mail Removed)> wrote in message
> news:466e60e7$0$30330$(E-Mail Removed)...
>> Hi, could somebody point me in the right direction for adding, removing
>> and modifying nodes in an xml file programatically using vb.net (.net
>> framework 2.0)? I have an xml file in the following format which I need to
>> be able to add and remove records from. (I can not change this xml file
>> format.)
>>
>> <country name="United Kingdom">
>> <city name="City1">
>> <street name="15 Street Address">
>> <details>
>> <postcode>POS COD</postcode>
>> <province>County1</province>
>> <c>GB</c>
>> </details>
>> </street>
>> </city>
>> <city name="City2">
>> <street name="10 street address">
>> <details>
>> <postcode>PST COD</postcode>
>> <province>County</province>
>> <c>GB</c>
>> </details>
>> </street>
>> </city>
>> </country>
>>
>>
>> I am having problems adding a node without overwriting existing data. For
>> example I can determine if a Country already exists in the XML file but
>> when I add the City and the Street items to it using the following code,
>> it replaces existing city and street data in the xml file rather than
>> appending new elements.
>>
>> Sub InsertAddress(ByVal doc As XmlDocument)
>> Dim addresses As XmlNode = doc.DocumentElement
>> Dim country As XmlElement
>>
>> If country_exists() Then
>> 'append
>> country = doc.SelectSingleNode("//country[@name='Country']")
>> Else
>> country = doc.CreateElement("country")
>> country.SetAttribute("name", "Spain")
>> End If
>>
>> Dim city As XmlElement = doc.CreateElement("city")
>> city.SetAttribute("name", "City3")
>>
>> Dim street As XmlElement = doc.CreateElement("street")
>> street.SetAttribute("name", "20 Street Address")
>>
>> Dim details As XmlElement = doc.CreateElement("details")
>> Dim postcode As XmlElement = doc.CreateElement("postcode")
>> postcode.InnerText = "POS COD"
>>
>> Dim province As XmlElement = doc.CreateElement("province")
>> province.InnerText = "County3"
>>
>> Dim c As XmlElement = doc.CreateElement("c")
>> c.InnerText = "UK"
>>
>> country.AppendChild(city)
>> city.AppendChild(street)
>> street.AppendChild(details)
>> details.AppendChild(postcode)
>> details.AppendChild(province)
>> details.AppendChild(c)
>> addresses.AppendChild(country)
>> End Sub
>>
>>
>> Thanks for any help you can give.
>> Mark.

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
escaped XML data writing/reading/parsing queries. Piper707@hotmail.com Java 1 12-12-2005 11:05 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Newbie needs a tutorial on reading and writing to nodes in an XML file David Hearn ASP .Net 1 06-30-2004 12:58 PM



Advertisments