Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : A newbie's cry for help editing XML document


GeorgeAtkins
2/14/2005 5:59:02 AM
HELP!

Using VB.NET, VS2003, XML. I apologize for the length of this request.

Though I've written before and received advise pointing to several articles
(which I've read and re-read), frankly, I'm still confused and need help!

My request might be a little different than what XML procedures appear to be
directed towards (ie, wholesale transformations or mass updates). What I need
to do is to selectively locate specific book titles and edit information in a
another field of the book's data.

Here is a simplified view of the XML file. It represents data for two
specific books. The document root is <mrcbfile>. The node for specific books
(with attribute) is <mrcb format-type="bd">. Each book node has several
childnodes, although I've only included a few for this example (capitalized
nodes are the important ones):

<mrcbfile>
<mrcb format-type="bd">
<mrcb-control-fields>
<mrcb008-bk>
<mrcb008-bk-00-05 value="020718"/>
</mrcb008-bk>
</mrcb-control-fields>
<MRCB-TITLE-AND-TITLE-RELATED>
<mrcb245 i1="i1-1" i2="i2-4">
<mrcb245-a>The fat girl</mrcb245-a>
<mrcb245-c>Marilyn Sachs.</mrcb245-c>
</mrcb245>
</mrcb-title-and-title-related>
<MRCB-NOTES>
<mrcb520 i1="i1-blank" i2="i2-blank">
<mrcb520-a>Jeff, a high school senior, becomes obsessed with creating a
beautiful person.</mrcb520-a>
</mrcb520>
</mrcb-notes>
<MRCB-NOTES>
<mrcb596 i1="i1-blank" i2="i2-blank">
<mrcb596-a>1</mrcb596-a>
</mrcb596>
</mrcb-notes>

</mrcb> <mrcb format-type="bd">
<mrcb-control-fields>
<mrcb008-bk>
<mrcb008-bk-00-05 value="020718"/>
</mrcb008-bk>
</mrcb-control-fields>
<MRCB-TITLE-AND-TITLE-RELATED>
<MRCB245 i1="i1-1" i2="i2-2">
<MRCB245-A>A pocket full of seeds </mrcb245-a>
</mrcb245>
</mrcb-title-and-title-related>
<MRCB-NOTES>
<mrcb520 i1="i1-blank" i2="i2-blank">
<mrcb520-a>Nicole's parents thought they would have enough time to
escape from the Nazis.</mrcb520-a>
</mrcb520>
</mrcb-notes>
<MRCB-NOTES>
<MRCB590 i1="i1-blank" i2="i2-blank">
<MRCB590-A>Quiz owned by: FOREST, NEILL</mrcb590-a>
</mrcb590>
<MRCB590 i1="i1-blank" i2="i2-blank">
<mrcb590-a>Quiz owned by: FOREST, NEILL</mrcb590-a>
</mrcb590>
</mrcb>
</mrcbfile>


What I need to do:
1. Navigate to/select a book (/<mrcb>) and check the book's title (at
/<mrcb>/<mrcb-title-and-title-related>/mrcb245/mrcb245-a)
2. If the title matches my compare string, I need to look for the books'
note tag
<mrcb590>/<mrcb590-a> which contains the text "Quiz owned by..." and
add a name to the end of the list. Not all books have the 590 tag.
3. Repeat steps 1 and 2 for every other book in the file.

PROBLEMS
1. I've tried using xpath and DOM, building nodelists to look into. But when
I build a reference to the book node, I get stuck on how to navigate through
each child and "grand-child" to find the title and notes descendants of that
node so I can change the Notes text. I've tried to create nexted "for each
loops" (as I would normally use), based on additional nodelists set to
deeper levels, but they don't seem to be connected to the main loop, making
unforeseen changes.
2. I'm thinking that, once I locate the first node (the first book), I will
need
to clone the node so that I can navigate through it without changing the
original current node location. But I'm stuck on how this all should be coded.
3. In short, I can locate the first level node, but I'm stuck on how to
properly drill down to the descendants of each node to make changes, without
losing my place.

REQUEST
I'm frustrated and desperate. Can somebody give me a bit of (pseudo or real
)code to show how the best way to navigate through the books <mrcb>, look for
a specific book title <mrcb245-a> and if found, append/edit text to any (and
all) 590-a text descendants ("QUiz owned by...") of that node.

I've read every articles I can find on xml, xpath, doc, navigator, etc. And
I admit it - I'm frustrated and desperate. It must be so blindingly obvious,
I'm not seeing it (shades of Edgar Allen Poe).

Thanks a million in advance!
George
GeorgeAtkins
2/14/2005 7:53:06 AM
Thanks much for the function and the quick reply, Marre!

I have also been able to return entire markups of current and childnodes,
though I did not use your specific coding technique at the time. I now see
how the enumerator should be used.

But here is where I need to go. For example:

Find the book title "A pocket full of seeds." This is located under the node
path: /<mrcb>/<mrcb-title-and-title-related>/<mrcb245>/<mrcb245-a>. Having
found it, edit the <mrcb590-a> child node of the same <mrcb> ancestor, to
append text:

BEFORE
<mrcb590-a>Quiz owned by: NEILL, SANDBURG</mrcb590-a>
AFTER
<mrcb590-a>Quiz owned by: NEILL, SANDBURG, PLANE</mrcb590-a>

So, I think I have a navigation issue. I tried the following. It finds the
title, but fails to make the edit (though it seems to work and there is no
error).

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click
Dim doc As XmlDocument = New XmlDocument
doc.Load("C:\AccentDocs\pmsrecs.xml")
Dim nodelist As XmlNodeList
Dim node As XmlNode
Dim BkTitle As String

BkTitle = "A pocket full of seeds /"
nodelist =
doc.SelectNodes("/mrcbfile/mrcb/mrcb-title-and-title-related[mrcb245/mrcb245-a='" & BkTitle & "']/following-sibling::mrcb-notes/mrcb590/descendant::text()")
'nodelist =
doc.SelectNodes("/mrcbfile/mrcb/mrcb-title-and-title-related/mrcb245/descendant::245-a")
MsgBox(nodelist.Count)
'If nodelist.Count > 0 Then
For Each node In nodelist
MsgBox(node.InnerText)
If InStr(node.InnerText, "pocket full of seeds") Then
node.InnerText += ", PLANE")
End If
Next
doc.PreserveWhitespace = True
Dim wtr As XmlTextWriter = New
XmlTextWriter("C:\AccentDocs\REVpmsrecs.xml", Encoding.Unicode)
doc.WriteTo(wtr)
wtr.Close()
MsgBox("All done!")
doc = Nothing
End
End Sub

Sorry to be so dense about this. It must be xml-anxiety or something.
George

[quoted text, click to view]
MA
2/14/2005 3:39:17 PM
Hi!

Try this out:
Private Function myFunction(ByVal strNodeName As String) As String
Dim myDoc As XmlDocument = New XmlDocument
Dim retval As String

Try
myDoc.Load("filepath to the xml document")

Dim root As XmlNode = myDoc.DocumentElement 'youre
<mrcbfile>-node
Dim ienum As IEnumerator = root.GetEnumerator()
Dim myNode As XmlNode

While (ienum.MoveNext())
myNode = CType(ienum.Current, XmlNode)
If myNode.Name = nodeName Then
retval = myNode.OuterXml
End If
End While

Return retval
End Function

Hope this helps!

/Marre

[quoted text, click to view]

AddThis Social Bookmark Button