0

I have basic VB programming knowledge. I'm having trouble getting info from an XML file. I know I can simply open the XML as a normal file, and read it sequentially, but I'd like to have my code running as efficient as possible.

I'm reading a bunch of XML files. The <full path>+<file name> will be passed to the xml-reading sub or function.

Here's what the XML looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <mproxyMessage xmlns:ns2="com.cmp.mproxy.message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:mproxyMessage">
    <version>2</version>
    <receiver>
        <uid>az.local</uid>
    </receiver>
    <sender>
        <uid>test.sender</uid>
    </sender>
    <attachments>
        <attachment>
            <uri>C:\Inbox\2015-09-04\2cc403ace4984782bf5ae949e22a4d5b\VJ14699062340.ima</uri>
        </attachment>
    </attachments>
    <option>
        <map>
            <element>
                <key>OPTION_MEXI_OPTION</key>
                <value>DSUBJECT(DMA-IMA)DGIVENNAME(9803375)DSENDERID(144796) medidoc_formatrecipient : Demo User, 14699062,Department,AZO(recipient DN : uid=demo.user,null)</value>
            </element>
            <element>
                <key>DSENDERID</key>
                <value>144796</value>
            </element>
            <element>
                <key>DGIVENNAME</key>
                <value>9803375</value>
            </element>
            <element>
                <key>OPTION_MESSAGE_UID</key>
                <value>2cc403ace4984782bf5ae949e22a4d5b</value>
            </element>
            <element>
                <key>DSUBJECT</key>
                <value>DMA-IMA</value>
            </element>
            <element>
                <key>recipientDN</key>
                <value>uid=demo.user</value>
            </element>
            <element>
                <key>recipientLabel</key>
                <value>Demo User, 14699062,Department,AZO</value>
            </element>
        </map>
    </option>
</mproxyMessage>

What I need in return (for further processing) is the value between the <uri> tags (a file path), the DSUBJECT value, and the recipientLabel (only the number, but that's easy to split)

The URI seems to be quite simple using:

Dim xml = XDocument.Load(file.FullName)
MsgBox("URI: " + xml.<mproxyMessage>.<attachments>.<attachment>.<uri>.Value)

But I'm stuck with the KEY/Value part

I have been looking at several code samples to do this, but none produced the result I was hoping for...

Is their someone who can provide me a code sample?

2 Answers 2

0

You can query for the key/value elements using LINQ (untested code):

Dim dsubject = (
    From e In xml.<mproxyMessage>.<option>.<map>.<element>
    Where e.<key>.Value = "DSUBJECT"
    Select e.<value>.Value
).Single()

This looks for the <element> with the matching <key> value, and selects the associated <value> value.

2
  • Great!! Worked 'out of the box' !! Only need to find out how to make this error proof (some XML's don't have the dsubject tag), but this is a perfect starting point) Commented Sep 7, 2015 at 16:22
  • You can use SingleOrDefault() to handle cases where there is no match. It will return Nothing if no match is found.
    – Mark
    Commented Sep 7, 2015 at 21:18
0

Try it without specifying the root element:

    'to load
    ' someXML = XElement.Load(PATH HERE)

    Dim someXML As XElement = <mproxyMessage xmlns:ns2="com.cmp.mproxy.message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:mproxyMessage">
                                  <version>2</version>
                                  <receiver>
                                      <uid>az.local</uid>
                                  </receiver>
                                  <sender>
                                      <uid>test.sender</uid>
                                  </sender>
                                  <attachments>
                                      <attachment>
                                          <uri>C:\Inbox\2015-09-04\2cc403ace4984782bf5ae949e22a4d5b\VJ14699062340.ima</uri>
                                      </attachment>
                                  </attachments>
                                  <option>
                                      <map>
                                          <element>
                                              <key>OPTION_MEXI_OPTION</key>
                                              <value>DSUBJECT(DMA-IMA)DGIVENNAME(9803375)DSENDERID(144796) medidoc_formatrecipient : Demo User, 14699062,Department,AZO(recipient DN : uid=demo.user,null)</value>
                                          </element>
                                          <element>
                                              <key>DSENDERID</key>
                                              <value>144796</value>
                                          </element>
                                      </map>
                                  </option>
                              </mproxyMessage>

    Dim s As String = someXML.<attachments>.<attachment>.<uri>.Value
1
  • I think you haven't got my question right :).. Getting the <uri> value is no problem. It's getting the value that corresponds with the DSUBJECT key, and the same for the recipientLabel key Commented Sep 6, 2015 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.