How to use XmlDocument?

J

Joe Duchtel

Hello -

I have been searching the Internet for some help with the XmlDocument
but have not found an example that really helps me.

What I essentially want to do is parse the following XML to extract
the offset and type fields of a parameter by specifying "ABC", "Data
A", "Field A" ...

<Configuration file="xyz.ini">
<Source name="ABC">
<Subset name="Data A">
<Parameter name="FieldA" offset="0" type="double">
<Parameter name="FieldB" offset="0" type="double">
<Parameter name="FieldC" offset="0" type="double">
...

I played around with the SelectNodes() but cannot figure out how to
specify the fields.

Any suggestions?

Thanks!
Joe
 
J

Joe Cool

Hello -

I have been searching the Internet for some help with the XmlDocument
but have not found an example that really helps me.

What I essentially want to do is parse the following XML to extract
the offset and type fields of a parameter by specifying "ABC", "Data
A", "Field A" ...

<Configuration file="xyz.ini">
  <Source name="ABC">
    <Subset name="Data A">
      <Parameter name="FieldA" offset="0" type="double">
      <Parameter name="FieldB" offset="0" type="double">
      <Parameter name="FieldC" offset="0" type="double">
      ...

I played around with the SelectNodes() but cannot figure out how to
specify the fields.

Any suggestions?

Well, for one thing, this is not a valid XML file. To fix it you need
to modify the Parameter nodes as in:

<Parameter name="FieldA" offset="0" type="double"/>

Also, you are only making it harder to process by using attributes
instead of nodes.
 
J

Joe Duchtel

Hello -

Thanks for the feedback ... I do have the /> at the end but forgot
them when I pasted into the post.

I know it doesn't seem right but I didn't come up with the *.xml file
itself ... I only have to process it. What is the easiest way to get
the information I need?

Thanks!
Joe
 
J

Joe Cool

Hello -

Thanks for the feedback ... I do have the /> at the end but forgot
them when I pasted into the post.

I know it doesn't seem right but I didn't come up with the *.xml file
itself ... I only have to process it.  What is the easiest way to get
the information I need?

OK, I'll do your work for you. I figured this out years ago and it
really wasn't all that hard.

Dim doc As XmlDocument
Dim nodelistSource As XmlNodeList
Dim nodelistSubset As XmlNodeList
Dim nodelistFields As XmlNodeList
Dim sourceName As XmlAttribute
Dim subsetName As XmlAttribute
Dim fieldName As XmlAttribute
Dim searchSource As String = "ABC"
Dim searchSubset As String = "Data A"
Dim searchField As String = "FieldA"

doc = New XmlDocument
doc.Load(System.Windows.Forms.Application.StartupPath &
"\Document1.XML")
nodelistSource = doc.SelectNodes("Configuration/Source")
For i = 0 To nodelistSource.Count - 1
sourceName = nodelistSource.Item(i).Attributes("name")
If sourceName.Value = searchSource Then
nodelistSubset = nodelistSource.Item(i).SelectNodes
("Subset")
For j = 0 To nodelistSubset.Count - 1
subsetName = nodelistSubset.Item(j).Attributes
("name")
If subsetName.Value = searchSubset Then
nodelistFields = nodelistSubset.Item
(j).SelectNodes("Parameter")
For k = 0 To nodelistFields.Count - 1
fieldName = nodelistFields.Item
(k).Attributes("name")
If fieldName.Value = searchField Then
' process node nodelistFields(k)
End If
Next
End If
Next
End If
Next
 
J

Joe Duchtel

OK, I'll do your work for you. I figured this out years ago and it
really wasn't all that hard.

        Dim doc As XmlDocument
        Dim nodelistSource As XmlNodeList
        Dim nodelistSubset As XmlNodeList
        Dim nodelistFields As XmlNodeList
        Dim sourceName As XmlAttribute
        Dim subsetName As XmlAttribute
        Dim fieldName As XmlAttribute
        Dim searchSource As String = "ABC"
        Dim searchSubset As String = "Data A"
        Dim searchField As String = "FieldA"

        doc = New XmlDocument
        doc.Load(System.Windows.Forms.Application.StartupPath &
"\Document1.XML")
        nodelistSource = doc.SelectNodes("Configuration/Source")
        For i = 0 To nodelistSource.Count - 1
            sourceName = nodelistSource.Item(i).Attributes("name")
            If sourceName.Value = searchSource Then
                nodelistSubset = nodelistSource.Item(i)..SelectNodes
("Subset")
                For j = 0 To nodelistSubset.Count - 1
                    subsetName = nodelistSubset.Item(j).Attributes
("name")
                    If subsetName.Value = searchSubset Then
                        nodelistFields = nodelistSubset.Item
(j).SelectNodes("Parameter")
                        For k = 0 To nodelistFields.Count - 1
                            fieldName = nodelistFields.Item
(k).Attributes("name")
                            If fieldName.Value = searchField Then
                                ' processnode nodelistFields(k)
                            End If
                        Next
                    End If
                Next
            End If
        Next




- Show quoted text -

Hello -

Thanks so much! I wasn't looking for the entire solution but rather a
few pointers to help me figure it out ... but this is great! You're
right ... it's not that hard ... once you know how to do it!

Thanks again,
Joe
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top