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>Count y1</province>
<c>GB</c>
</details>
</street>
</city>
<city name="City2">
<street name="10 street address">
<details>
<postcode>PST COD</postcode>
<province>Count y</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(B yVal doc As XmlDocument)
Dim addresses As XmlNode = doc.DocumentEle ment
Dim country As XmlElement
If country_exists( ) Then
'append
country = doc.SelectSingl eNode("//country[@name='Country']")
Else
country = doc.CreateEleme nt("country")
country.SetAttr ibute("name", "Spain")
End If
Dim city As XmlElement = doc.CreateEleme nt("city")
city.SetAttribu te("name", "City3")
Dim street As XmlElement = doc.CreateEleme nt("street")
street.SetAttri bute("name", "20 Street Address")
Dim details As XmlElement = doc.CreateEleme nt("details")
Dim postcode As XmlElement = doc.CreateEleme nt("postcode")
postcode.InnerT ext = "POS COD"
Dim province As XmlElement = doc.CreateEleme nt("province")
province.InnerT ext = "County3"
Dim c As XmlElement = doc.CreateEleme nt("c")
c.InnerText = "UK"
country.AppendC hild(city)
city.AppendChil d(street)
street.AppendCh ild(details)
details.AppendC hild(postcode)
details.AppendC hild(province)
details.AppendC hild(c)
addresses.Appen dChild(country)
End Sub
Thanks for any help you can give.
Mark.