简体   繁体   English

解析MapQuest XML响应

[英]parse MapQuest XML response

here is the xml returned : 这是返回的xml:

<IncidentsResponse>
  <Incidents>
    <Incident>
      <id>920959670</id>
      <type>1</type>
      <severity>3</severity>
      <eventCode>701</eventCode>
      <lat>35.91411</lat>
      <lng>-86.82417</lng>
      <startTime>2014-03-01T01:00:00.000-05:00</startTime>
      <endTime>2016-06-16T00:59:00.000-04:00</endTime>
      <shortDesc>
        I-65 : Maintenance work between Exit 59 TN-840 and Exit 65 TN-96
      </shortDesc>
      <fullDesc>
        Intermittent lane closures due to maintenance work on I-65 both ways between Exit 59     TN-840 and Exit 65 TN-96.
      </fullDesc>
      <delayFromTypical>0.0</delayFromTypical>
      <delayFromFreeFlow>0.0</delayFromFreeFlow>
      <distance>12.09</distance>
      <iconURL>https://api.mqcdn.com/mqtraffic/const_mod.png</iconURL>
    </Incident>
  </Incidents>
</IncidentsResponse>

I want to extract this into two brief descriptors, obtained from shortDesc and severity. 我想将其提取为两个简短的描述符,它们是从shortDesc和严重性获得的。 Here is my C# attempt so far : 到目前为止,这是我的C#尝试:

string[] incidentsDscrp = { };
string[] severity = { };
int count = 0;


XDocument doc = XDocument.Parse(traffData);

foreach (var incidents in doc.Descendants("Incident"))
{
    incidentsDscrp[count] = incidents.Element("shortDesc").Value;
    severity[count] = incidents.Element("severity").Value;
    count++; 
}

trafficLabel.Text = "";
for (int a = 0; a < incidentsDscrp.Length; a++)
{
    trafficLabel.Text += incidentsDscrp[a];
    trafficLabel.Text += severity[a];
}

Basically I want to store descriptions of incidents in the incidentsDscrp array and severity level in the severity array, and then add them as text to a winforms label. 基本上,我想将事件的描述存储在eventssDscrp数组中,并将严重性级别存储在严重性数组中,然后将它们作为文本添加到winforms标签中。

Edit : My error is that index was outside bounds of the array at the 编辑:我的错误是索引是在数组的边界之外

severity[count] = incidents.Element("severity").value;

line 线

Use following to parse and create a label - 使用以下内容解析和创建标签-

 from c in trafficData.Descendants("Incident")       
    select new
    {
        ShortDescription = c.Element("shortDesc").Value,
        Severity = c.Element("severity").Value
    }).Aggregate((a,b) => a.ShortDescription +": " + a.Severity +", " 
                        + b.ShortDescription +": " + b.Severity);

Sorry to say, but you're doing it wrong. 不好意思,但您做错了。

First of all, storing data like that (in parallel collections), is a really bad idea. 首先,存储这样的数据(并行收集)是一个非常糟糕的主意。 You should read following blog post by Jon Skeet: Anti-pattern: parallel collections . 您应该阅读Jon Skeet的以下博客文章: 反模式:并行集合

Now, to solve your problem. 现在,解决您的问题。 Here is a little LINQ to XML query which allows you to get all you need from XML document: 这是一些LINQ to XML查询,它使您可以从XML文档中获得所需的一切:

var items = from i in xDoc.Root.Element("Incidents").Elements("Incident")
            select new
            {
                ShortDescription = (string)i.Element("shortDesc"),
                Severity = (int)i.Element("severity")
            };

You can join all the items into a string using String.Join : 您可以使用String.Join将所有项目连接到一个字符串中:

var text = String.Join(",", items.Select(x => string.Format("{0}: {1}", x.ShortDescription.Trim(), x.Severity.ToString())));

For your sample document, text s value is: 对于您的示例文档, text的值为:

I-65 : Maintenance work between Exit 59 TN-840 and Exit 65 TN-96: 3 I-65:在59 TN-840出口和65 TN-96出口之间的维护工作:3

Change separator of format string to get desired result. 更改格式字符串的分隔符以获得所需的结果。 Now you can assign it to the label: 现在,您可以将其分配给标签:

trafficLabel.Text = text;

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

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