Retrieve specific part of XML tree value

徘徊边缘 提交于 2020-01-16 09:23:20

问题


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.


回答1:


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



回答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;



回答3:


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



回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/57401428/retrieve-specific-part-of-xml-tree-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!