簡體   English   中英

VB.Net XML解析問題

[英]VB.Net XML Parsing Issue

我在將一些XML信息解析到Richtextbox時遇到了一些困難。 我正在從流中提取XML文件,並且數據成功通過,但是當我嘗試查看元素時,我認為存在問題。

這是流加載后的代碼片段(每個表單名稱旁邊通常都有值)

“ <form name =” SupportRepID“> 1234 </ form>

<form name =“ SupportRepName”>約翰·史密斯</ form>

<form name =“ SupportRepKey”> XXXXX-XXXXX-XXXXX-XXXXX-XXXXX </ form>

<form name =“ DepartmentID”> 1 </ form>

<form name =“ DepartmentName”>常規</ form>

<form name =“ DepartmentCurrency”> $ </ form>“

我的最終目標是能夠獲取SupportRepKey和SupportRepName。

我認為問題出在<details>下的每個名稱都沒有歸類為Element。 當我運行這樣的代碼以嘗試獲取“ SupportRepKey”的值時,消息框將顯示一個空值(空白)。

Dim reader As XDocument = XDocument.Load(dataStream)



    For Each detail As XElement In reader...<details>
        Dim APIKey As String = detail.Element("SupportRepKey")
        MessageBox.Show(APIKey) 'returns null value
    Next

如果我以此方式運行它(用於故障排除,以確保從dataStream中提取XML文件):

 Dim reader As XDocument = XDocument.Load(dataStream)

    For Each detail As XElement In reader...<details>
        MessageBox.Show(detail.Value) 'returns all values in one long string
    Next

它在一個長字符串中返回每個<表單名稱=“”>旁邊的所有值:“ 1234JohnSmithXXXXX-XXXXX-XXXXX-XXXXX-XXXXX1General $”

我正在使用API​​與我工作的系統進行交互。 每當有人輸入其登錄信息並且每個代表都有一個單獨的密鑰時,便會生成RepName和RepKey。 我需要能夠僅抓住這兩件事並將它們設置為變量或文本字段中的文本,以便我可以進一步與每個用戶進行API交互。

我認為這與<details>形式為name =“”下的對象有關,但是過去兩天我到處都在尋找,沒有幫助。 也許我不是在搜索正確的術語,但是我喜歡這個網站。 每個人總是很樂於助人,所以我想我會碰碰運氣的。 謝謝!

屬性是您需要的,不能完全確定對代碼的更改是什么,但是查看屬性的更改將允許您提取此數據。

detail.Element( “形式”)。屬性( “名稱”)

您想要的元素是“表單”,其屬性為 “名稱”,其值為“ SupportRepKey”。

您正在做的是:找到元素“ SupportRepKey”。

detail.Element("SupportRepKey")

但這不存在,因為元素名稱為“ form”。 “ SupportRepKey”只是名為“ name”的屬性的值。

如何使用XPath?

C#:

string repKey = reader.XPathSelectElement("//form[@name='SupportRepKey']").Value;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM