如何用c#在asp.net中读取在线xml节点 [英] How to read online xml node in asp.net with c#

查看:71
本文介绍了如何用c#在asp.net中读取在线xml节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想通过此在线xml阅读国家/地区节点:



http://ws.cdyne.com /ip2geo/ip2geo.asmx/ResolveIP?IPaddress=49.14.147.196&LicenseKey=0 [ ^ ]



见我试过的内容 -

 XmlDocument document =  new  XmlDocument(); 
document.Load( http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP ?名称IP地址= 49.14.147.196&安培; LICENSEKEY = 0\" );
XmlNode node = document.SelectSingleNode( / IPInformation / Country);
string name = node.InnerText;





但不是成功,在字符串名称中获取错误 - 对象引用未设置为对象的实例,XmlNode节点也为空。

解决方案

返回的XML文档具有默认命名空间。不幸的是, XmlDocument 并不容易选择具有默认命名空间的节点。您需要创建 XmlNamespaceManager ,为默认命名空间添加名称空间前缀,并更改XPath表达式以使用新的名称空间前缀:

< pre lang =cs> XmlDocument document = new XmlDocument();
document.Load( http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP ?名称IP地址= 49.14.147.196&安培; LICENSEKEY = 0\" );

XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
namespaceManager.AddNamespace( x,document.DocumentElement.NamespaceURI);

XmlNode node = document.SelectSingleNode( / x:IPInformation / x:Country,namespaceManager);
string name = node.InnerText;


Hi There,

I want to read Country node from this online xml :

http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=49.14.147.196&LicenseKey=0[^]

See What I Tried -

XmlDocument document = new XmlDocument();
document.Load("http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=49.14.147.196&LicenseKey=0"); 
XmlNode node = document.SelectSingleNode("/IPInformation/Country");
string name = node.InnerText;



But not succeeded, Getting Error in string name - Object reference not set to an instance of an object, XmlNode node is also null.

解决方案

The returned XML document has a default namespace. Unfortunately, the XmlDocument doesn't make it easy to select nodes with a default namespace. You need to create an XmlNamespaceManager, add a namespace prefix for the default namespace, and change your XPath expression to use the new namespace prefix:

XmlDocument document = new XmlDocument();
document.Load("http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?IPaddress=49.14.147.196&LicenseKey=0"); 

XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
namespaceManager.AddNamespace("x", document.DocumentElement.NamespaceURI);

XmlNode node = document.SelectSingleNode("/x:IPInformation/x:Country", namespaceManager);
string name = node.InnerText;


这篇关于如何用c#在asp.net中读取在线xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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