0

Have an Xml.XmlNodeList variable "xmlNodes", populated with data. The xmlNodes(0).InnerText contains the following:

<InitializePagedPull ShowCode="AR13dfD">
   <TokenInfo PageToken="293845657-32-47" TotalRecords="1" PageSize="20" TotalPages="1" />
</InitializePagedPull>

I'd like to get the TokenInfo.PageToken value... without having to use some archaic method of "string.indexOf() string.substring(x,y)"

I've tried creating a child XmlDocument based on the current xmlNodes(0).InnerText... and then using GetElementsByTagName("TokenInfo")... which gets me a little closer... but I'm still unable to easily grab the PageToken value within.

adam
  • 2,930
  • 7
  • 54
  • 89
  • 1
    Can you use xpath? – Jack Fleeting Aug 07 '19 at 19:55
  • @JackFleeting If I knew what it was, or how... probably :-P Quick google tells me it's like RegEx but for XML ? – adam Aug 07 '19 at 19:57
  • 1
    No, it's far, far more than that, and it's not at all like regex (fortunately...). If you can use xpath (see for example https://learn.microsoft.com/en-us/dotnet/standard/data/xml/select-nodes-using-xpath-navigation), a simple one line expression will extract your token. – Jack Fleeting Aug 07 '19 at 20:06
  • Do you want vb.net or c#? – djv Aug 07 '19 at 20:11
  • @djv Either one... easy to swap back and forth... but already got an answer below :-) – adam Aug 07 '19 at 20:13
  • 1
    @adam yes, that works. You can still check out serialization which allows you to model your data with concrete classes to give you strong-typing. You can make a model of your entire xml file and deserialize into classes to access any property much more simply. – djv Aug 07 '19 at 20:15
  • @djv Absolutely, once I get the basics working I plan on cleaning it up with full blown objects , mapped for each field , etc... baby steps, lol. – adam Aug 07 '19 at 20:17

5 Answers5

2

Using XPath you can query the xml document to pull out information you want.

string xml = @"<InitializePagedPull ShowCode=""AR13dfD""><TokenInfo PageToken=""293845657-32-47"" TotalRecords=""1"" PageSize=""20"" TotalPages=""1""/></InitializePagedPull>";

// Load the XML into an XmlDocument
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

// Using the query "/InitializePagedPull/TokenInfo/@PageToken" we can pull out the @PageToken attribute
var pageToken = xmlDoc.SelectSingleNode("/InitializePagedPull/TokenInfo/@PageToken").Value;
Matt
  • 1,749
  • 2
  • 12
  • 26
2

The answer that is using XPath is still archaic. It is already more than a decade since LINQ to XML rules them all. Here we go.

c#

void Main()
{
    XElement xml = XElement.Parse(@"<InitializePagedPull ShowCode=""AR13dfD""><TokenInfo PageToken=""293845657-32-47"" TotalRecords=""1"" PageSize=""20"" TotalPages=""1""/></InitializePagedPull>");
    string pageToken = xml.Descendants().Attributes("PageToken").FirstOrDefault().Value;
}
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
1

Using XML serialization,

Create classes which represent your data

<Serializable>
Public Class InitializePagedPull
    <XmlElement>
    Public Property TokenInfo As TokenInfo
End Class

Class TokenInfo
    <XmlAttribute>
    Public Property PageToken As String
    <XmlAttribute>
    Public Property TotalRecords As Integer
    <XmlAttribute>
    Public Property PageSize As Integer
    <XmlAttribute>
    Public Property TotalPages As Integer
End Class

Then assuming your xml is an XElement,

Dim xml = <InitializePagedPull ShowCode="AR13dfD">
              <TokenInfo PageToken="293845657-32-47" TotalRecords="1" PageSize="20" TotalPages="1"/>
          </InitializePagedPull>
Dim serializer As New XmlSerializer(GetType(InitializePagedPull))
Dim pull = DirectCast(serializer.Deserialize(xml.CreateReader()), InitializePagedPull)
Dim pageToken = pull.TokenInfo.PageToken
djv
  • 15,168
  • 7
  • 48
  • 72
1

You can use XPath to retrieve an xml attribute value. I had a quick search and found this, any help? Getting attribute using XPath

Michael
  • 473
  • 5
  • 12
-1

Probably there is a namespace related things. You can try with a LocalName property instead of name.

Claudio
  • 3,060
  • 10
  • 17