使用C#解析XML

时间:2012-10-09 22:33:58

标签: c# xmlreader

我从ARIN获得了这个XML响应:

<nets xmlns="http://www.arin.net/whoisrws/core/v1" xmlns:ns2="http://www.arin.net/whoisrws/rdns/v1" xmlns:ns3="http://www.arin.net/whoisrws/netref/v2" termsOfUse="https://www.arin.net/whois_tou.html">
    <limitExceeded limit="256">false</limitExceeded>
    <net termsOfUse="https://www.arin.net/whois_tou.html">
        <registrationDate>2003-05-09T16:14:30-04:00</registrationDate>
        <ref>http://whois.arin.net/rest/net/NET-216-113-160-0-1</ref>
        <ns2:delegations termsOfUse="https://www.arin.net/whois_tou.html">
            <ns2:delegationRef name="186.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/186.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="165.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/165.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="173.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/173.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="189.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/189.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="169.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/169.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="167.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/167.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="171.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/171.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="166.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/166.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="190.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/190.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="188.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/188.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="176.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/176.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="172.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/172.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="170.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/170.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="191.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/191.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="174.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/174.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="162.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/162.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="185.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/185.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="180.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/180.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="168.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/168.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="175.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/175.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="161.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/161.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="179.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/179.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="177.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/177.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="183.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/183.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="181.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/181.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="187.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/187.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="163.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/163.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="184.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/184.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="160.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/160.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="182.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/182.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="178.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/178.113.216.in-addr.arpa.</ns2:delegationRef>
            <ns2:delegationRef name="164.113.216.in-addr.arpa.">http://whois.arin.net/rest/rdns/164.113.216.in-addr.arpa.</ns2:delegationRef>
        </ns2:delegations>
        <endAddress>216.113.191.255</endAddress>
        <handle>NET-216-113-160-0-1</handle>
        <name>EBAY-QA-IT-1</name>
        <netBlocks>
            <netBlock>
                <cidrLength>19</cidrLength>
                <endAddress>216.113.191.255</endAddress>
                <description>Direct Assignment</description>
                <type>DS</type>
                <startAddress>216.113.160.0</startAddress>
            </netBlock>
        </netBlocks>
        <pocs termsOfUse="https://www.arin.net/whois_tou.html"/>
        <orgRef name="eBay, Inc" handle="EBAY">http://whois.arin.net/rest/org/EBAY</orgRef>
        <parentNetRef name="NET216" handle="NET-216-0-0-0-0">http://whois.arin.net/rest/net/NET-216-0-0-0-0</parentNetRef>
        <startAddress>216.113.160.0</startAddress>
        <updateDate>2012-03-02T08:03:18-05:00</updateDate>
        <version>4</version>
    </net>
</nets>

我正在尝试获取此信息:

http://whois.arin.net/rest/org/EBAY

从这一行:

<orgRef name="eBay, Inc" handle="EBAY">http://whois.arin.net/rest/org/EBAY</orgRef>

如何使用C#获取此信息?

3 个答案:

答案 0 :(得分:7)

使用Linq-Xml(可在.Net 3.5及更高版本中使用),您可以使用以下代码片段来完成此操作:

XNamespace xns = "http://www.arin.net/whoisrws/core/v1"; //The default xmlns from the root element.

var orgRefHandle =
  XDocument.Parse(xml)
  .Root
  .Element(xns + "net")
  .Element(xns + "orgRef")
  .Attribute("handle").Value;

答案 1 :(得分:4)

一种方法是使用XmlDocument

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);

foreach(XmlElement ele in xmlDoc.DocumentElement.GetElementsByTagName("net"))
{
    // Writes value to console.
    Console.WriteLine(((XmlElement)ele.GetElementsByTagName("orgRef")).Attributes["handle"].Value);
}

答案 2 :(得分:0)

将内容加载到XmlDocument中,然后您可以使用XPath查询来使用SelectSingleNode获取节点的属性:/nets/limitexceeded/net/orgref/@Name并获取值。