我的任务是从 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">ü96ü0ýý10232632ü4Hü1</S><S Idx="2">ü96ü0ýý10232632ü4Hü1</S><S Idx="3">ü96ü0ýý10232632ü4Hü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 或其他值。
我意识到这可能会或可能不会影响其他用户,但希望我不是唯一一个遇到这个问题的人......