Home All Groups Group Topic Archive Search About
Author
10 May 2005 1:04 PM
Scott M. Lyon
What is going to be the best way to update an XML file from VB6?


Basically, I've got an XML file that contains data such as this (sample
data, of course):

<RootElement>
    <Row>
        <Column1>Data1</Column1>
        <Column2>Data2</Column2>
        <Column3>Data3</Column3>
    </Row>
    <Row>
        <Column1>Data4</Column1>
        <Column2>Data5</Column2>
        <Column3>Data6</Column3>
    </Row>
</RootElement>


I need to implement functionality to do two things:

1) Delete a particular "Row" element (and all sub-elements), based on the
data in one or more of the Column sub-elements
2) Add a new "Row" element with the three column elements populated


At this point, I don't believe I'd need to update existing "Row" elements
(other than specified above), and I figure even if I did, I could always
read the "Row" into memory, delete the "Row", update the data, and then add
it again as a new "Row".


What is going to be the easiest way to implement Delete and Add, considering
this XML is stored in a file (let's say it's stored at C:\Temp\Data.xml)?



Thanks!

Author
10 May 2005 1:32 PM
Larry Serflaten
"Scott M. Lyon" <scott.RED.lyon.WH***@rapistan.BLUE.com> wrote
> What is going to be the best way to update an XML file from VB6?

By using MSFT XML parser.

Add a reference to Microsoft XML 4.0 (or whatever the latest version you
have), and check out what it contains using VB's Object Browser.  Specificially
note what's available in the DOMDocument40 (or whatever the latest...)

You'd use CreateElement to create the nodes in the shape you want them,
and AppendChild to add them in where they need to go.

You might also want to read the docs on RemoveChild and ReplaceChild.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmmthremovechild.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmmthreplaceChild.asp
(They have VB examples)

LFS
Author
10 May 2005 3:10 PM
Peter Aitken
Show quote Hide quote
"Scott M. Lyon" <scott.RED.lyon.WH***@rapistan.BLUE.com> wrote in message
news:efY2TDWVFHA.2128@TK2MSFTNGP15.phx.gbl...
> What is going to be the best way to update an XML file from VB6?
>
>
> Basically, I've got an XML file that contains data such as this (sample
> data, of course):
>
> <RootElement>
>    <Row>
>        <Column1>Data1</Column1>
>        <Column2>Data2</Column2>
>        <Column3>Data3</Column3>
>    </Row>
>    <Row>
>        <Column1>Data4</Column1>
>        <Column2>Data5</Column2>
>        <Column3>Data6</Column3>
>    </Row>
> </RootElement>
>
>
> I need to implement functionality to do two things:
>
> 1) Delete a particular "Row" element (and all sub-elements), based on the
> data in one or more of the Column sub-elements
> 2) Add a new "Row" element with the three column elements populated
>
>
> At this point, I don't believe I'd need to update existing "Row" elements
> (other than specified above), and I figure even if I did, I could always
> read the "Row" into memory, delete the "Row", update the data, and then
> add it again as a new "Row".
>
>
> What is going to be the easiest way to implement Delete and Add,
> considering this XML is stored in a file (let's say it's stored at
> C:\Temp\Data.xml)?
>

MS provides a free XML library called MSXML. It has functions for just about
anything you would want to do with XML. While yuo can manipulate XML using
VB's text commands it is not recommended.


--
Peter Aitken

Remove the crap from my email address before using.
Author
10 May 2005 3:44 PM
Mike D Sutton
Show quote Hide quote
> What is going to be the best way to update an XML file from VB6?
>
> Basically, I've got an XML file that contains data such as this (sample
> data, of course):
>
> <RootElement>
>    <Row>
>        <Column1>Data1</Column1>
>        <Column2>Data2</Column2>
>        <Column3>Data3</Column3>
>    </Row>
>    <Row>
>        <Column1>Data4</Column1>
>        <Column2>Data5</Column2>
>        <Column3>Data6</Column3>
>    </Row>
> </RootElement>
>
>
> I need to implement functionality to do two things:
>
> 1) Delete a particular "Row" element (and all sub-elements), based on the
> data in one or more of the Column sub-elements
> 2) Add a new "Row" element with the three column elements populated
>
>
> At this point, I don't believe I'd need to update existing "Row" elements
> (other than specified above), and I figure even if I did, I could always
> read the "Row" into memory, delete the "Row", update the data, and then
> add it again as a new "Row".
>
>
> What is going to be the easiest way to implement Delete and Add,
> considering this XML is stored in a file (let's say it's stored at
> C:\Temp\Data.xml)?

Select the node you want to kill by using a XPath query, then remove that
node using RemoveChild()
Author
10 May 2005 4:14 PM
Mike D Sutton
> What is going to be the best way to update an XML file from VB6?
>
> Basically, I've got an XML file that contains data such as this (sample
> data, of course):
<snip>
Show quoteHide quote
> I need to implement functionality to do two things:
>
> 1) Delete a particular "Row" element (and all sub-elements), based on the
> data in one or more of the Column sub-elements
> 2) Add a new "Row" element with the three column elements populated
>
> At this point, I don't believe I'd need to update existing "Row" elements
> (other than specified above), and I figure even if I did, I could always
> read the "Row" into memory, delete the "Row", update the data, and then
> add it again as a new "Row".
>
> What is going to be the easiest way to implement Delete and Add,
> considering this XML is stored in a file (let's say it's stored at
> C:\Temp\Data.xml)?

Use an XPath query to select the node with the contents you're interested
in, then if found remove it:

'***
Dim MyDOM As DOMDocument
Dim SearchNode As IXMLDOMNode

Const FileName As String = "X:\Path\File.xml"

Set MyDOM = New DOMDocument
MyDOM.async = False
If (MyDOM.Load(FileName)) Then
    Set SearchNode = MyDOM.selectSingleNode( _
        "RootElement/Row[Column1/text()=""Data4""]")
    If (Not (SearchNode Is Nothing)) Then _
        Call SearchNode.parentNode.removeChild(SearchNode)

    Debug.Print MyDOM.xml
End If
Set MyDOM = Nothing
'***

Adding a node is dead easy, simply create some new nodes from the DOM and
add them into the appropriate place sin the tree.  Here's a link to some old
posts of mine on using XML in VB which should answer all your questions on
that:
http://groups.google.co.uk/group/microsoft.public.vb.general.discussion/msg/c8f73ad9971fd89
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/