使用唯一编号的类解析XML

时间:2016-05-11 14:21:39

标签: xml vb.net xml-parsing

嘿所有我有以下代码,我需要找到 类:

Dim nodelist As System.Xml.XmlNodeList = Nothing
Dim doc As New System.Xml.XmlDocument()

doc.LoadXml(tmpData)
nodelist = doc.SelectNodes("//entry/content/sp_0:div/span/sp_0:span")

For Each node As System.Xml.XmlElement In nodelist
   Debug.print(node("OrderID").InnerText)
Next

XML看起来像这样:

<feed
    xmlns:app="http://www.w3.org/2007/app"
    xmlns:thr="http://purl.org/syndication/thread/1.0"
    xmlns:fh="http://purl.org/syndication/history/1.0"
    xmlns:snx="http://www.ibm.com/xmlns/prod/sn"
    xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"
    xmlns="http://www.w3.org/2005/Atom">
    <id>2006:feed</id>
    <generator version="5.0.0.0" uri="http://www.ibm.com/xmlns/prod/sn">IBM Connections - Profiles</generator>
    <title type="text">reporting chain for Bill Gates</title>        
    <opensearch:itemsPerPage>8</opensearch:itemsPerPage>
    <fh:complete></fh:complete>
    <link
        href="http://..."
        rel="self"
        type="application/atom+xml"></link>
    <entry>
        <id>tag:profiles.ibm.com,2006</id>
        <title type="text">Bill Gates</title>
        <updated>2016-05-11T06:39:54.908Z</updated>
        <category term="profile" scheme="http://www.ibm.com/xmlns/prod/sn/type"></category>
        <contributor>
            <name>Bill Gates</name>
            <snx:userid>010101</snx:userid>
            <email>Bill_Gates@microsoft.com</email>
            <snx:userState>active</snx:userState>
            <snx:isExternal>false</snx:isExternal>
        </contributor>
        <link
            href="http://..."
            rel="http://www.ibm.com/xmlns/prod/sn/profile-type"
            type="application/profile-type+xml"></link>
        <thr:in-reply-to>
            <app:accept>ref</app:accept>
            <app:accept>tag:profiles.ibm.com,2006</app:accept>
        </thr:in-reply-to>
        <summary type="text">Profile information for Bill Gates</summary>
        <content type="xhtml">
            <sp_0:div
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:sp_0="http://www.w3.org/1999/xhtml">
                <sp_0:span class="vcard">
                    <sp_0:div class="x-groupwareMail" style="display:none"></sp_0:div>
                    <sp_0:div class="org">
                        <sp_0:span class="organization-unit"></sp_0:span>
                    </sp_0:div>
                    <sp_0:div class="role"></sp_0:div>
                    <sp_0:div class="title">Applications Developer/Analyst</sp_0:div>
                    <sp_0:div class="uid">265418</sp_0:div>
                    <sp_0:div class="x-profile-uid">010101</sp_0:div>
                    <sp_0:div class="x-lconn-userid">265418</sp_0:div>
                </sp_0:span>
            </sp_0:div>
        </content>
    </entry>
    <entry>
        etc....            
    </entry>
</feed>

我在 nodelist = doc.SelectNodes(&#34; // entry / content / sp_0:div / span / sp_0:span&#34;)上收到错误说:

  

需要命名空间管理器或XsltContext。此查询具有前缀,变量或用户定义的函数。

如何从该课程中获取内部文字 010101

2 个答案:

答案 0 :(得分:1)

您需要使用XmlNamespaceManager类来定义XPath中使用的命名空间,并将其传递给SelectNodes方法。另外,我无法获得一个空命名空间来使用默认命名空间(可能是XPath的限制?),因此必须为此定义一个前缀。

此外,您的XPath似乎与您的示例XML不匹配。假设你想获得<sp_0:div class="x-profile-uid">010101</sp_0:div>元素的值,你会想要这样的东西:

Dim nodelist As System.Xml.XmlNodeList = Nothing
Dim doc As New System.Xml.XmlDocument()

doc.LoadXml(tmpData)

Dim nsmgr = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom")
nsmgr.AddNamespace("sp_0", "http://www.w3.org/1999/xhtml")

nodelist = doc.SelectNodes(
    "//atom:entry/atom:content/sp_0:div/sp_0:span/sp_0:div[@class='x-profile-uid']",
    nsmgr
)

For Each node As System.Xml.XmlElement In nodelist
   Debug.Print(node.InnerText)
Next

哪个输出:

010101

希望您可以根据自己的需要进行调整。

答案 1 :(得分:0)

您应该定义名称空间“sp_0”,就像在根元素中定义其他名称空间(xmlns:app =“...”)一样。

查看元素的最后一行:这是如何为SP_0定义命名空间。

    <feed
xmlns:app="http://www.w3.org/2007/app"
xmlns:thr="http://purl.org/syndication/thread/1.0"
xmlns:fh="http://purl.org/syndication/history/1.0"
xmlns:snx="http://www.ibm.com/xmlns/prod/sn"
xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"
xmlns="http://www.w3.org/2005/Atom"
sp_0="http://www.w3.org/1999/xhtml">

<feed xmlns:app="http://www.w3.org/2007/app" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:fh="http://purl.org/syndication/history/1.0" xmlns:snx="http://www.ibm.com/xmlns/prod/sn" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.w3.org/2005/Atom" sp_0="http://www.w3.org/1999/xhtml">