adam
adam

Reputation: 2970

Retrieve specific part of XML tree value

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.

Upvotes: 0

Views: 126

Answers (5)

Yitzhak Khabinsky
Yitzhak Khabinsky

Reputation: 22187

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;
}

Upvotes: 2

Michael
Michael

Reputation: 563

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

Upvotes: 1

Matt
Matt

Reputation: 1757

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;

Upvotes: 2

djv
djv

Reputation: 15774

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

Upvotes: 1

Claudio
Claudio

Reputation: 3095

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

Upvotes: -1

Related Questions