XML帮助请 - 下载一个文件,我不知道如何阅读它 [英] XML Help please - Downloaded a file and I have no clue how to read it
问题描述
我有这个文件已下载
文件从这里开始..
<? xml < span class =code-summarycomment> version = 1.0 编码 = ISO-8859-1 ? >
- < HostipLookupResultSet version = 1.0.1 xmln s:gml = http://www.opengis.net/gml < span class =code-attribute> xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation = http://www.hostip.info/api/hostip-1.0.1 .xsd >
< gml:description > 这是Hostip Lookup Service < / gml:description >
< gml:name > hostip < span class =code-keyword>< / gml:name >
- < gml:boundedBy >
< gml:Null > inapplicable < / gml:Null >
< / gml:boundedBy >
- < gml:featureMember < span class =code-keyword>>
- < Hostip >
< ip > 107.6.97.20 < / ip >
< gml:name > (未知城市?)< / gml:name >
< ; countryName > (未知国家?)< / countryName >
< countryAbbrev > XX < / countryAbbrev >
- <! - 坐标不可用
- >
< / Hostip >
< / gml:featureMember >
< / HostipLookupResultSet >
文件在这里结束...
我用这段代码捕获它
使用 client = 新 WebClient()
Dim strFile = client.DownloadString( String .Format(<跨越ss =code-string> http://api.hostip.info/?ip= + IpAddress))
如果这里的数据我只需要..
< ip > 107.6.97.20 < / ip >
< gml:name > (未知城市?)< / gml:name < span class =code-keyword>>
< countryName > (未知国家?)< / countryName &g t;
< countryAbbrev > XX < / countryAbbrev >
我不知道如何捕获城市,国家和代码并填充文本字段。 />
如果有人能给我一个如何做到这一点的简单例子,我将非常感激。
谢谢,
-Ron
它看起来像XML。 :-)
您可以使用.NET FCL提供的任何XML解析器。这是我对它们的简短回顾:
- 使用
System.Xml.XmlDocument
类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。- 使用类
System.Xml.XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。- 使用类
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 [ ^ ]。
使用System.Xml.XmlDataDocument
:
首要元素: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument%28v=vs.110%29.aspx [ ^ ],
to得到任何元素的孩子:
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.childnodes%28v=vs.110%29.aspx [ ^ ],
http:// msdn .microsoft.com / zh-cn / library / system.xml.xmlnodelist%28v = vs.110%29.aspx [ ^ ],
得到孩子:< a href =http://msdn.microsoft.com/en-us/library/system.xml.xmlnodelist.item%28v=vs.110%29.aspx> http://msdn.microsoft.com/en -us /库/ system.xml.xmlnodelist.it em%28v = vs.110%29.aspx [ ^ ]。
-SA
这很简单:
Dim IpAddress as String =
Dim client as 新 WebClient()
Dim strFile as string = client.DownloadString( String .Format( http://api.hostip.info/?ip= {0},IpAddress))
Dim xml as XDocument = XDocument.Parse(strFile)
Dim xnm as 新 XmlNamespaceManager( new NameTable())
xnm.AddNamespace( gml, http://www.opengis.net/gml)
Console.WriteLine(xml.XPathSelectElement( // ip,xnm).Value)
Console.WriteLine(xml.XPathSelectElement( // Hostip / gml:name,xnm).Value)
Console.WriteLine(xml.XPathSelectElement( // countryName,xnm).Value)
Console.WriteLine(xml.XPathSelectElement( // countryAbbrev,xnm).Value)
I have this file Downloaded
File starts here..
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <HostipLookupResultSet version="1.0.1" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">
<gml:description>This is the Hostip Lookup Service</gml:description>
<gml:name>hostip</gml:name>
- <gml:boundedBy>
<gml:Null>inapplicable</gml:Null>
</gml:boundedBy>
- <gml:featureMember>
- <Hostip>
<ip>107.6.97.20</ip>
<gml:name>(Unknown City?)</gml:name>
<countryName>(Unknown Country?)</countryName>
<countryAbbrev>XX</countryAbbrev>
- <!-- Co-ordinates are unavailable
-->
</Hostip>
</gml:featureMember>
</HostipLookupResultSet>
File Ends here...
I Capture it with this code
Using client = New WebClient()
Dim strFile = client.DownloadString(String.Format("http://api.hostip.info/?ip=" + IpAddress))
All I need if the data here..
<ip>107.6.97.20</ip>
<gml:name>(Unknown City?)</gml:name>
<countryName>(Unknown Country?)</countryName>
<countryAbbrev>XX</countryAbbrev>
I have no idea how to capture the City, Country and Code and populate a text field.
If someone could provide me a simple example of how to do this, I would be most grateful.
Thanks,
-Ron
It looks like XML. :-)
You can use any of the XML parsers offered by .NET FCL. This is my short review of them:
- 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[^].- 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[^].- Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, 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[^].
[EDIT: in response to follow-up questions]
WithSystem.Xml.XmlDataDocument
:
Top element: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument%28v=vs.110%29.aspx[^],
to get any element's children:
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.childnodes%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.xml.xmlnodelist%28v=vs.110%29.aspx[^],
to get child: http://msdn.microsoft.com/en-us/library/system.xml.xmlnodelist.item%28v=vs.110%29.aspx[^].
—SA
It is simple as that:
Dim IpAddress as String = "" Dim client as New WebClient() Dim strFile as string = client.DownloadString(String.Format("http://api.hostip.info/?ip={0}",IpAddress)) Dim xml as XDocument = XDocument.Parse(strFile) Dim xnm as New XmlNamespaceManager(new NameTable()) xnm.AddNamespace("gml", "http://www.opengis.net/gml") Console.WriteLine(xml.XPathSelectElement("//ip", xnm).Value) Console.WriteLine(xml.XPathSelectElement("//Hostip/gml:name", xnm).Value) Console.WriteLine(xml.XPathSelectElement("//countryName", xnm).Value) Console.WriteLine(xml.XPathSelectElement("//countryAbbrev", xnm).Value)
这篇关于XML帮助请 - 下载一个文件,我不知道如何阅读它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!