1

我的任务是从 XML POST 响应中检索特定数据。

以下是 XML 回复的示例:

<ServiceHistoryArray>
    <RecordID>XD116067*401529</RecordID>
    <SWR>
        <V Idx="1">7932</V><V Idx="2">7932</V><V Idx="3">7932</V><V Idx="4">7932</V>
    </SWR>
    <Comments/>
    <Mileage>6</Mileage>
    <Operation>
        <V Idx="1">Z7000*WPDIP****PRE-DELIVERY INSPECTION - BASE TIME</V><V Idx="2">Z911*INC****ETCH WINDOWS</V><V Idx="3">B0048*WPBS4****FASCIA, FRONT BUMPER - REPLACE ONE PIECE</V><V Idx="4">9997*WPBS4****</V>
    </Operation>
    <CloseDate>1998-12-30</CloseDate>
    <OpenDate>1998-10-16</OpenDate>
    <PartsAmount>
        <V Idx="1">0.00</V><V Idx="2">0.00</V><V Idx="3">166.32</V><V Idx="4">0.00</V>
    </PartsAmount>
    <LaborAmount>
        <V Idx="1">64.80</V><V Idx="2">3.60</V><V Idx="3">140.40</V>
    </LaborAmount>
    <Warranty>
        <S Idx="1">&#xfc;96&#xfc;0&#xfd;&#xfd;10232632&#xfc;4H&#xfc;1</S><S Idx="2">&#xfc;96&#xfc;0&#xfd;&#xfd;10232632&#xfc;4H&#xfc;1</S><S Idx="3">&#xfc;96&#xfc;0&#xfd;&#xfd;10232632&#xfc;4H&#xfc;1</S>
    </Warranty>
</ServiceHistoryArray>  

我需要连接所有相关的“V”元素的内部文本。

例如:
7392 Z700*WDIP****交付前检查 - 基准时间 0.00 64.80
7392 Z911*INC****蚀刻窗口 0.00 3.60
等...

到目前为止,这是我的代码:

// String variable to store the XML POST Reply
   string replyString = WebRequestPostData("http://" + maskedTextBox1.Text + ":" + maskedTextBox2.Text, payload);


    XmlDocument replyDoc = new XmlDocument();  // New XmlDocument from reply
    replyDoc.LoadXml(replyString);
    XmlNodeList list = replyDoc.SelectNodes("//ServiceHistoryArray/*");  // XmlNodeList based on child elements of <ServiceHistoryArray> node.

    var nodees = new List<XmlNode>(list.Cast<XmlNode>().ToArray());

    // Finds Mileage, CloseDate, and OpenDate values and displays them on form.
    foreach (XmlNode node in nodees)
    {
        switch (node.Name)
        {
            case "OpenDate":
                textBox1.Text = node.InnerText;
                break;
            case "CloseDate":
                textBox2.Text = node.InnerText;
                break;
            case "Mileage":
                textBox3.Text = node.InnerText;
                break;
        }
    }

    // This section is supposed to display the concatenated values of the <V> elements:
    for (int i = 0; i < nodees.Count; i++)
    {
        if (nodees[i].HasChildNodes)
        {
            for (int j = 0; j < nodees[i].ChildNodes.Count; j++)
            {
                switch (nodees[i].ChildNodes[j].NodeType)
                {
                    case XmlNodeType.Text:
                        break;
                    case XmlNodeType.Element:
                        switch (nodees[i].ChildNodes[j].Attributes[0].Value)
                        {

                            case "1":
                                Console.WriteLine("1-" + nodees[i].ChildNodes[j].InnerText + " ");
                                break;
                            case "2":
                                Console.WriteLine("2-" + nodees[i].ChildNodes[j].InnerText + " ");
                                break;
                            case "3":
                                Console.WriteLine("3-" + nodees[i].ChildNodes[j].InnerText + " ");
                                break;
                            case "4":
                                Console.WriteLine("4-" + nodees[i].ChildNodes[j].InnerText + " ");
                                break;
                        }
                        break;
                }
            }
        }
    }

连接的值将需要显示在 listBox 控件中。此外,我需要尝试对这个过程进行编码,而不是预期一定数量的元素;在这种情况下,有 4 个。POST 请求的其他一些实例可能会返回 10 或 2 或其他值。

我意识到这可能会或可能不会影响其他用户,但希望我不是唯一一个遇到这个问题的人......

4

1 回答 1

2

您可以使用 LINQ to XML:

XDocument xdoc = XDocument.Parse(replyString);
var query = from v in xdoc.Descendants("V")
            group v by (int)v.Attribute("Idx") into g
            select String.Join(" ", g.Select(x => (string)x));

此查询选择文档中的所有V元素并按属性值对它们进行分组Idx(按元素出现在文档中的顺序)。然后将组中元素的所有值连接成字符串。查询将包含:

"7932 Z7000*WPDIP****PRE-DELIVERY INSPECTION - BASE TIME 0.00 64.80"
"7932 Z911*INC****ETCH WINDOWS 0.00 3.60"
"7932 B0048*WPBS4****FASCIA, FRONT BUMPER - REPLACE ONE PIECE 166.32 140.40"
"7932 9997*WPBS4**** 0.00"
于 2013-07-23T22:09:31.917 回答