简体   繁体   English

在C#中获取错误的XML值解析

[英]Getting Wrong Value Parsing XML in C#

Environment: VS 2010, .NET 3.5 环境:VS 2010,.NET 3.5

I'm working on a project that pulls data from a RESTful web service. 我正在研究一个从RESTful Web服务提取数据的项目。 Almost all of it is working correctly except one section where it's getting incorrect values. 几乎所有功能都可以正常工作,只是其中一部分获得的值不正确。

Here's a sample of the XML used in this part of the code: 这是此部分代码中使用的XML的示例:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<dataprovider>
  <id>DP6</id>
  <name>Query 1</name>
  <dataSourceId>96390</dataSourceId>
  <dataSourcePrefix>DS6</dataSourcePrefix>
  <updated>2014-08-25T16:51:38.000-04:00</updated>
  <duration>1</duration>
  <isPartial>false</isPartial>
  <rowCount>13</rowCount>
  <flowCount>1</flowCount>
  <dictionary>
    <expression qualification="Dimension" dataType="String">
      <id>DP6.DOa6</id>
      <name>City</name>
      <description>City located.</description>
      <dataSourceObjectId>DS6.DOa6</dataSourceObjectId>
      <formulaLanguageId>[City]</formulaLanguageId>
    </expression>
  </dictionary>
</dataprovider>

And here is the code: 这是代码:

private void loadElements(List<string> dps, int rptId)
{
  string name = string.Empty;
  string id = string.Empty;
  string elementUrl = baseUrl + string.Format(C_WEBI_URL, rptId) + C_DP_URL;
  foreach (string dpId in dps)
  {
    XmlDocument dpXml = getResponse(elementUrl + "/" + dpId);
    //get the Expression nodes that define the objects on the report
    XmlNodeList elementNodes = dpXml.SelectNodes("//expression");
    if (elementNodes != null)
    {
      foreach (XmlNode node in elementNodes)
      {
        id = node.SelectSingleNode("//dataSourceObjectId").InnerText;
        if (!elements.ContainsKey(id))
        {
          name = node.SelectSingleNode("//name").InnerText;
          elements.Add(id, name);
        }
      }
    }
  }
}

I expect to get a value of "DP6.DOa6" from the dataSourceObjectId, but it returns just "DP6". 我希望从dataSourceObjectId获得一个值“ DP6.DOa6”,但它仅返回“ DP6”。 And I expect to get a value of "City" from the name, but it returns "Query 1". 而且我希望从名称中获得一个“ City”的值,但是它将返回“ Query 1”。 When I test the XPath with the above XML at http://www.xpathtester.com/xpath , I get the correct values, but it's not working in my code. 当我在http://www.xpathtester.com/xpath上使用上述XML测试XPath时,我得到了正确的值,但是在我的代码中不起作用。

Any thoughts? 有什么想法吗?

The problem with your name query is that the XmlNode object retains the outer xml as well so when you say //name it will return the first name element it finds in the document. 名称查询的问题在于XmlNode对象也保留了外部xml,因此当您说//name ,它将返回它在文档中找到的第一个name元素。

The correct xpath syntax will be name (no slashes). 正确的xpath语法应为name (无斜杠)。

As to the dataSourceObjectId my suspicion is that the . 至于dataSourceObjectId我怀疑是. is causing this behavior perhaps try InnerXml instead of InnerText 导致此行为,请尝试使用InnerXml而不是InnerText

Complete example: 完整的例子:

foreach (XmlNode node in elementNodes)
  {
    id = node.SelectSingleNode("dataSourceObjectId").InnerXml; //I'm not sure if this will solve the problem.
    if (!elements.ContainsKey(id))
    {
      name = node.SelectSingleNode("name").InnerText;
      elements.Add(id, name);
    }
  }

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

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