从具有多个名称空间的xml消息中读取元素值

时间:2014-02-17 09:04:47

标签: xml vb.net linq

我是Linq to XML的新手。我试图在此消息中获取MSGID节点的元素值:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header>
        <nsah:AuditHeader xmlns:nsah="http://schemas.rnab.nl/giraal/AuditHeader/1.0">
            <To>mq://rifb/accounting/AP</To>
            <RelatesTo>mq://rnab/giraal/PE/BookingStatusAPS/01</RelatesTo>
            <Action>mq://rifb/accounting/AP/BookingService/01</Action>
            <From>mq://rnab/giraal/PE</From>
        </nsah:AuditHeader>
    </soapenv:Header>
    <soapenv:Body>
        <ns5:CREATEPMNT_FSFS_REQ xsi:schemaLocation="http://fcubs.ofss.com/service/FCUBSCPGServices CommPmntGtwyCreate-Req-Full.xsd" xmlns:ns5="http://fcubs.ofss.com/service/FCUBSCPGServices">
            <ns5:FCUBS_HEADER>
                <ns5:SOURCE>SOURCE</ns5:SOURCE>
                <ns5:UBSCOMP>FCUBS</ns5:UBSCOMP>
                <ns5:MSGID>MsgiD01236549876546351321</ns5:MSGID>
                <ns5:USERID>UId</ns5:USERID>
                <ns5:BRANCH>BRANCH</ns5:BRANCH>
                <ns5:MODULEID>MId</ns5:MODULEID>
                <ns5:SERVICE>SERVICE</ns5:SERVICE>
                <ns5:OPERATION>Operation</ns5:OPERATION>
                <ns5:DESTINATION>Destination</ns5:DESTINATION>
            </ns5:FCUBS_HEADER>
            <ns5:FCUBS_BODY>
                <ns5:PmntDETAILS>
                    <ns5:XREF>XREF321654987</ns5:XREF>
                    <ns5:AMOUNT>0.09000</ns5:AMOUNT>
                    <ns5:VALUE_DATE>2012-12-20</ns5:VALUE_DATE>
                    <ns5:CCY>EUR</ns5:CCY>
                    <ns5:CUST_AC_NO>123456789</ns5:CUST_AC_NO>
                    <ns5:CPTY_AC_NO>987654321</ns5:CPTY_AC_NO>      
                </ns5:PmntDETAILS>
            </ns5:FCUBS_BODY>
        </ns5:CREATEPMNT_FSFS_REQ>
    </soapenv:Body>
</soapenv:Envelope>

这就是我现在所拥有的:

Dim XDoc As XDocument = XDocument.Load(Variable_Echo.OpenedFile)
Dim XNs_soapenv As XNamespace = "http://schemas.xmlsoap.org/soap/envelope/"
Dim XNs_ns5 = "http://fcubs.ofss.com/service/FCUBSCPGServices"
Dim Body = XDoc.Descendants(XNs_soapenv + "Body")
Dim MsgId = Body.Descendants(XNs_ns5 + "MSGID")

当我调试我的代码时,我在MsgId =“附加信息时得到以下错误:':'字符,十六进制值0x3A,不能包含在名称中。”

非常感谢帮助

1 个答案:

答案 0 :(得分:2)

您隐式将XNs_ns5声明为String。因此:XNs_ns5 + "MSGID"表示连接字符串,表示元素名称而不是前缀+元素名称。因为元素名称永远不应包含冒号(:),该表达式将触发错误。将XNs_ns5的声明更改为XNamespace,并且错误将会出现:

Dim XNs_ns5 As XNamespace = "http://fcubs.ofss.com/service/FCUBSCPGServices"