在VB.net中使用XPath的具有特定属性的Access元素

时间:2016-07-23 08:51:14

标签: xml vb.net xpath namespaces

由于使用XPath获取具有特定属性的元素太多时间,我正在努力。

下面是我想从中获取元素的wsdl:

    <?xml version="1.0" encoding="UTF-8"?>

<!-- Copyright Zuora, Inc. 2007 - 2010 All Rights Reserved. -->

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:zns="http://api.zuora.com/" 
    xmlns:ons="http://object.api.zuora.com/"
    xmlns:fns="http://fault.api.zuora.com/"
    targetNamespace="http://api.zuora.com/">
    <types>


<schema attributeFormDefault="qualified" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://object.api.zuora.com/">
                <import namespace="http://api.zuora.com/" />
                <complexType name="zObject">
                <sequence>
                    <element minOccurs="0" maxOccurs="unbounded" name="fieldsToNull" nillable="true" type="string" />
                    <element minOccurs="0" maxOccurs="1" name="Id" nillable="true" type="zns:ID" />
                </sequence>
            </complexType>


            <complexType name="AccountingCode" >
                <complexContent>
                    <extension base="ons:zObject">
                        <sequence>
                            <element minOccurs="0" name="Category" nillable="true" type="string" />
                            <element minOccurs="0" name="CreatedById" nillable="true" type="zns:ID" />
                            <element minOccurs="0" name="CreatedDate" nillable="true" type="dateTime" />
                            <element minOccurs="0" name="GLAccountName" nillable="true" type="string"   />
                            <element minOccurs="0" name="GLAccountNumber" nillable="true" type="string"   />
                            <element minOccurs="0" name="Name" nillable="false" type="string" />
                            <element minOccurs="0" name="Notes" nillable="true" type="string" />
                            <element minOccurs="0" name="Status" nillable="true" type="string" />
                            <element minOccurs="0" name="Type" nillable="false" type="string" />
                            <element minOccurs="0" name="UpdatedById" nillable="true" type="zns:ID" />
                            <element minOccurs="0" name="UpdatedDate" nillable="true" type="dateTime" />
                        </sequence>
                    </extension>
                </complexContent>
            </complexType>

            <complexType name="AccountingPeriod" >
                <complexContent>
                    <extension base="ons:zObject">
                        <sequence>
                            <element minOccurs="0" name="CreatedById" nillable="true" type="zns:ID" />
                            <element minOccurs="0" name="CreatedDate" nillable="true" type="dateTime" />
                            <element minOccurs="0" name="EndDate" nillable="true" type="date" />
                            <element minOccurs="0" name="FiscalYear" nillable="true" type="int" />
                            <element minOccurs="0" name="Name" nillable="true" type="string" />
                            <element minOccurs="0" name="Notes" nillable="true" type="string" />
                            <element minOccurs="0" name="StartDate" nillable="true" type="date" />
                            <element minOccurs="0" name="Status" nillable="true" type="string" />
                            <element minOccurs="0" name="UpdatedById" nillable="true" type="zns:ID" />
                            <element minOccurs="0" name="UpdatedDate" nillable="true" type="dateTime" />
                        </sequence>
                    </extension>
                </complexContent>
            </complexType>
        </schema>
    </types>
</definitions>

此wsdl中有多个名称空间定义:

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" 
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:zns="http://api.zuora.com/" 
xmlns:ons="http://object.api.zuora.com/"
xmlns:fns="http://fault.api.zuora.com/"
targetNamespace="http://api.zuora.com/">

这是我非常简单的代码。我很确定我的问题是在命名空间上,但尽管搜索了大量的解决方案我无法解决它:

Public Sub constituteLocalDictionnary()

    Dim pathWsdlFile As String
    pathWsdlFile = My.Settings.ZuoraUrlWSDL

    Dim doc As New XmlDocument()
    doc.Load(pathWsdlFile)



    Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
    nsmgr.AddNamespace("ns", "http://schemas.xmlsoap.org/wsdl/")
    nsmgr.AddNamespace("ons", "http://object.api.zuora.com/")

    Dim myXmlPath As String
    myXmlPath = "//ns:complexType[@name='" & ZuoraWsdlObjectsList(1) & "']"

    Dim root As XmlElement = doc.DocumentElement
    Dim node As XmlNode = root.SelectSingleNode(myXmlPath, nsmgr)

End Sub

2 个答案:

答案 0 :(得分:0)

您使用了错误的命名空间来引用complexType元素。 complexType实际上从其父元素schema而不是根元素继承了默认命名空间,因为父级更接近因此从外部元素上下文覆盖默认命名空间:

<schema attributeFormDefault="qualified" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://object.api.zuora.com/">
    <import namespace="http://api.zuora.com/" />
    <complexType name="zObject">
        ....
    </complexType>
    ....
</schema>       

尝试这种方式:

....

Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ns2", "http://www.w3.org/2001/XMLSchema")

Dim myXmlPath As String
myXmlPath = "//ns2:complexType[@name='" & ZuoraWsdlObjectsList(1) & "']"

答案 1 :(得分:0)

代码非常简单地使用Xml Linq

Imports System.Xml
Imports System.Xml.Linq

Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim doc As XDocument = XDocument.Load(FILENAME)
        Dim results = doc.Descendants().Where(Function(x) x.Name.LocalName = "complexType").Select(Function(y) New With { _
                .name = y.Attribute("name"),
                .elements = y.Descendants(y.Name.Namespace + "element").Select(Function(z) New With { _
                     .minOccurs = CType(z.Attribute("minOccurs"), Integer), _
                     .name = CType(z.Attribute("name"), String), _
                     .nillable = CType(z.Attribute("nillable"), Boolean), _
                     .type = CType(z.Attribute("type"), String) _
                 }).ToList()
        }).ToList()
    End Sub

End Module