简体   繁体   English

解析URL / Web服务

[英]Parsing URL/web-service

I made a request to a third party API and it gives me the following response in XML. 我向第三方API发出了请求,它以XML给出了以下响应。

<?xml version="1.0" ?>
<abc>
<xyz>
<code>-112</code>
<message>No such device</message>
</xyz>
</abc>

I read this using this code. 我使用此代码阅读。

 XmlDocument doc = new XmlDocument();
    doc.Load("*** url ***");

    XmlNode node = doc.SelectSingleNode("/abc/xyz");
    string code = node.SelectSingleNode("code").InnerText;
    string msg = node.SelectSingleNode("message").InnerText;

    Response.Write("Code: " + code);
Response.Write("Message: "+ msg);

But I get an error on this line: 但是我在这条线上出现错误:

string code = node.SelectSingleNode("code").InnerText;

Error is: 错误是:

Object reference not set to an instance of an object. 你调用的对象是空的。

I changed the first line of your XML file into: 我将XML文件的第一行更改为:

<?xml version="1.0"?>

to make it valid XML. 使其成为有效的XML。 With this change, your code works for me. 进行此更改后,您的代码对我有用。 Without the change, the parser throws an exception. 没有更改,解析器将引发异常。

As you've given it, 如您所给, there doesn't seem to be anything wrong with your code 您的代码似乎没有任何问题 Edit : Your declaration is wrong, as svinja pointed out, and your xml won't even load into the XmlDocument. 编辑:您的声明是错误的,正如svinja指出的那样,您的xml甚至不会加载到XmlDocument中。

However, I'm guessing that your xml is more complicated, and there is at least one namespace involved, which would cause the select to fail. 但是,我猜测您的xml更复杂,并且至少涉及一个名称空间,这将导致选择失败。

It isn't pretty, but what you can do is use namespace agnostic xpath to locate your nodes to avoid using a XmlNamespaceManager : 它不是很漂亮,但是您可以使用与名称空间无关的xpath来定位您的节点,以避免使用XmlNamespaceManager

XmlDocument doc = new XmlDocument();
doc.Load("*** url ***");

XmlNode node = doc.SelectSingleNode("/*[local-name()='abc']/*[local-name()='xyz']");
string code = node.SelectSingleNode("*[local-name()='code']").InnerText;
string msg = node.SelectSingleNode("*[local-name()='message']").InnerText;

Response.Write("Code: " + code);
Response.Write("Message: "+ msg);

Edit - Elaboration 编辑-详细说明

Your code works fine if you correct the declaration to <?xml version="1.0"?> 如果将声明更正为<?xml version="1.0"?>则您的代码可以正常工作

However, if you introduce namespaces into the mix, your code will fail unless you use namespace managers appropriately. 但是,如果将名称空间引入混合,除非您正确使用名称空间管理器,否则代码将失败。

My agnostic xpath above will also parse an xml document like so: 我上面不可知的xpath也将像这样解析xml文档:

<?xml version="1.0"?>
<abc xmlns="foo">
    <xyz xmlns="bar">
        <code xmlns="bas">-112</code>
        <message xmlns="xyz">No such device</message>
    </xyz>
</abc>

You can use LINQ to XML (if confortable): 您可以使用LINQ to XML(如果可以的话):

  XDocument doc = XDocument.Load(url);
  var selectors = (from elements in  doc.Elements("abc").Elements("xyz")
                  select elements).FirstOrDefault();
  string code = selectors.Element("code").Value;
  string msg = selectors.Element("message").Value;
[XmlRoot("abc")]
public class Entity
{
    [XmlElement("xyz")]
    public SubEntity SubEntity { get; set; }
}

public class SubEntity
{
    [XmlElement("code")]
    public string Code { get; set; }
    [XmlElement("message")]
    public string Message { get; set; }
}

And use standart xmlserializer 并使用标准的xmlserializer

       var xmlSerializer = new XmlSerializer(typeof(Entity));
       var result = xmlSerializer.Deserialize(new XmlTextReader("*** url ***"));
       Response.Write("Code: " + result.SubEntity.Code);
       Response.Write("Message: "+ result.SubEntity.Message);
<?xml version="1.0">
<abc>
  <xyz>
    <code>-112</code>
    <message> No such device </message>
  </xyz>
</abc>

try to set a list: 尝试设置一个列表:

XmlNodeList nodeList = root.SelectNodes("/abc/xyz");

then read all the nodes and get their text: 然后读取所有节点并获取其文本:

foreach(XmlNode node in nodeList)
{
  if(node.Name == "code")
  {
    string code = node.InnerText;
  }
  else
  if(node.Name == "message")
  {
    string msg = node.InnerText;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM