简体   繁体   中英

VB.Net XML Parsing Issue

I am having some difficulty parsing some XML information in to a richtextbox. I am pulling an XML file from a stream and the data is coming through successfully but when I try to view the elements I think there is a problem.

Here is a snippet of the stream after it has been loaded(there are usually values next to each form name)

"< form name="SupportRepID">1234< /form>

< form name="SupportRepName">John Smith< /form>

< form name="SupportRepKey">XXXXX-XXXXX-XXXXX-XXXXX-XXXXX< /form>

< form name="DepartmentID">1< /form>

< form name="DepartmentName">General< /form>

< form name="DepartmentCurrency">$< /form>"

My end goal is to be able to grab the SupportRepKey and the SupportRepName.

the problem I think lies with each name under < details > not being classified as an Element. When I run the code like this to try and get the value for "SupportRepKey" the messagebox will show a null value(blank).

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

If I run it with this(for troubleshooting to make sure the XML file is being pulled from the dataStream):

 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

It returns all the values next to each < form name=""> in one long string: "1234JohnSmithXXXXX-XXXXX-XXXXX-XXXXX-XXXXX1General$"

I am using an API to interact with system where I work. The RepName and RepKey are generated anytime someone enters their log in information and every rep has a seperate key. I need to be able to grab just these two things and set them as variables or as text in a text field so I can further interact with API per user.

I think it has something to do with the objects under < details > being form name="" but I have looked everywhere for the past two days and come to no help. Maybe I'm not searching for the right terms, but I love this site. Everyone is always so helpful so I guess I'll try my luck. Thanks!

Attributes is what your after, not fully sure what the change to your code would be but a change to looking at the attributes would allow you to pull out this data.

detail.Element("form").Attribute("name")

The Element you want is "form" which has an Attribute "name" which has the value "SupportRepKey".

What you are doing is: Find the Element "SupportRepKey".

detail.Element("SupportRepKey")

But this does not exist, cause the Element name is "form". "SupportRepKey" is just the value of an attribute called "name".

How about using an XPath?

C#:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM