我的任务是从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 **** ETCH WINDOWS 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或其他任何内容。
我意识到这可能会或可能不会影响其他用户,但希望我不是唯一有这个问题的用户...
答案 0 :(得分: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"