XML File - reading and writing

Discussion in 'ASP .Net' started by maw, Jun 12, 2007.

  1. maw

    maw Guest

    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">
    <postcode>POS COD</postcode>
    <city name="City2">
    <street name="10 street address">
    <postcode>PST COD</postcode>

    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
    country = doc.SelectSingleNode("//country[@name='Country']")
    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"

    End Sub

    Thanks for any help you can give.
    maw, Jun 12, 2007
    1. Advertisements

  2. maw

    RSH Guest

    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


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


    MyBase.Dataset.Tables(0).PrimaryKey = New DataColumn()

    Return MyBase.Dataset

    Catch exc As Exception





    End Try

    End Function

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

    Dim fs As System.IO.StreamWriter


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

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

    oDs.WriteXml(fs, XmlWriteMode.WriteSchema)

    Catch exc As Exception





    End Try

    End Function

    End Class

    Hope that helps,

    RSH, Jun 12, 2007
    1. Advertisements

  3. 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, Jun 12, 2007
  4. maw

    mark Guest

    Thanks RSH, can I still use this method with the XML file structure I
    posted, i.e using attributes?
    mark, Jun 12, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.