如何从xml获得价值? [英] How to get value from xml ?

查看:80
本文介绍了如何从xml获得价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<entry>
  <id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:84944023&amp;ga:userType=New+Visitor&amp;start-date=2014-04-21&amp;end-date=2014-04-22</id>
  <updated>2014-04-21T18:53:25.426Z</updated>
  <title type="text">ga:userType=New Visitor</title>
  <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
  <dxp:dimension name="ga:userType" value="New Visitor"/>
  <dxp:metric name="ga:sessions" type="integer" value="16"/>
 </entry>

 <entry>
  <id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:84944023&amp;ga:userType=Returning+Visitor&amp;start-date=2014-04-21&amp;end-date=2014-04-22</id>
  <updated>2014-04-21T18:53:25.426Z</updated>
  <title type="text">ga:userType=Returning Visitor</title>
  <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
  <dxp:dimension name="ga:userType" value="Returning Visitor"/>
  <dxp:metric name="ga:sessions" type="integer" value="23"/>
 </entry>







从xml以上我想获得值

< dxp:metric name =ga:sessionstype =integervalue =16 />










From above xml i want to get the value of
<dxp:metric name="ga:sessions" type="integer" value="16"/>

and

<dxp:metric name="ga:sessions" type="integer" value="23"/>
 </entry>





如何获得来自条目标签的这些值。 xml中有两个条目标记。



How to get these value from entry tag . There are two entry tag in a xml.

推荐答案

您需要解析XML。 .NET FCL提供了不同的方法,我在下面简要回顾:

You need to parse XML. .NET FCL offers different ways of doing it, which I review in brief below:


  1. 使用 System.Xml。 XmlDocument 类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].



另一个选项取决于如何定义 XML模式。如果你能够以最适合你的应用程序的方式自己定义它,那么最好的方法就是不使用XML serialization 来自己处理XML;最通用和最容易使用的方法是使用数据合同 http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx [ ^ ]。



-SA


Another options depends on how your XML schema is defined. If you can define it yourself the way most suitable for your application, the best approach would be not dealing with XML by yourself by using XML serialization; and the most universal and easiest to use approach would be using Data Contracts: http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

—SA


这篇关于如何从xml获得价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆