我有基本的VB编程知识。我无法从XML文件中获取信息。我知道我可以简单地将XML作为普通文件打开,并按顺序读取它,但我希望我的代码尽可能高效地运行。
我正在阅读一堆XML文件。 <full path>+<file name>
将传递给xml-reading子或函数。
这是XML的样子:
<?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>
我需要的回报(进一步处理)是<uri>
标签(文件路径),DSUBJECT
值和recipientLabel
之间的值(仅限数字,但这很容易分裂)
URI似乎很简单:
Dim xml = XDocument.Load(file.FullName)
MsgBox("URI: " + xml.<mproxyMessage>.<attachments>.<attachment>.<uri>.Value)
但我坚持使用KEY / Value部分
我一直在寻找几个代码示例,但没有一个产生我希望的结果......
他们的人能为我提供代码示例吗?
答案 0 :(得分:0)
尝试不指定根元素:
'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 :(得分:0)
您可以使用LINQ(未经测试的代码)查询键/值元素:
Dim dsubject = (
From e In xml.<mproxyMessage>.<option>.<map>.<element>
Where e.<key>.Value = "DSUBJECT"
Select e.<value>.Value
).Single()
这会查找具有匹配<element>
值的<key>
,并选择关联的<value>
值。