473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

XML File - reading and writing

maw
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.
Jun 12 '07 #1
3 1650
RSH
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" <ma**@nospam.comwrote in message
news:46***********************@news.zen.co.uk...
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.

Jun 12 '07 #2
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/
Jun 12 '07 #3
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" <ma**@nospam.comwrote in message
news:46***********************@news.zen.co.uk...
>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.

Jun 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.