下面我有我從Web服務接收到的XML。我在C#中使用XML真的很新,但我試圖僅在<data_text>
元素來自<form_id>
。我想通過遍歷所有的XML來寫入SQL發送的內容。有沒有簡單的方法來設置這個,所以我可以找到<data_text>
說從一個特定的<field_number>
並分配給一個字符串,然後使用該字符串寫入SQL?一旦我可以將所有東西都寫入字符串,我可以輕鬆地寫入SQL,但是我無法將XML讀入字符串。我也願意接受其他選擇。在最底層,我擁有目前C#的位置,就我而言,目前爲止。在C中讀取多個XML元素並寫入SQL#
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE pnet_message_history_packet_response PUBLIC>
<pnet_message_history_packet_response>
<packet_id>2</packet_id>
<imessage>
<vehicle_number>Test1</vehicle_number>
<created_datetime>02/20/2017 19:33:28</created_datetime>
<received_datetime>02/20/2017 19:33:53</received_datetime>
<recipient>
<recip_uid>1234</recip_uid>
<recip_name>TestRecip</recip_name>
</recipient>
<msn>1233</msn>
<base_msn>1234</base_msn>
<message_type>form</message_type>
<formdata>
<form_id>55555</form_id>
<im_field>
<field_number>5</field_number>
<empty_at_start>no</empty_at_start>
<driver_modified>no</driver_modified>
<data>
<data_text>Test5</data_text>
</data>
</im_field>
<im_field>
<field_number>6</field_number>
<empty_at_start>yes</empty_at_start>
<driver_modified>yes</driver_modified>
<data>
<data_text>Test6</data_text>
</data>
</im_field>
<im_field>
<field_number>7</field_number>
<empty_at_start>yes</empty_at_start>
<driver_modified>yes</driver_modified>
<data>
<data_text>Test7</data_text>
</data>
</im_field>
<im_field>
<field_number>8</field_number>
<empty_at_start>yes</empty_at_start>
<driver_modified>yes</driver_modified>
<data>
<data_date-time>09/09/09 09:09:00</data_date-time>
</data>
</im_field>
<im_field>
<field_number>9</field_number>
<empty_at_start>yes</empty_at_start>
<driver_modified>yes</driver_modified>
<data>
<data_date-time>09/09/09 09:09:00</data_date-time>
</data>
</im_field>
<im_field>
<field_number>10</field_number>
<empty_at_start>no</empty_at_start>
<driver_modified>no</driver_modified>
<data>
<data_text>Test10</data_text>
</data>
</im_field>
</formdata>
</imessage>
<imessage>
<vehicle_number>Test1</vehicle_number>
<created_datetime>02/20/2017 19:34:04</created_datetime>
<received_datetime>02/20/2017 19:34:19</received_datetime>
<recipient>
<recip_uid>1234</recip_uid>
<recip_name>TestRecip</recip_name>
</recipient>
<msn>1235</msn>
<base_msn>1236</base_msn>
<message_type>form</message_type>
<formdata>
<form_id>55555</form_id>
<im_field>
<field_number>5</field_number>
<empty_at_start>no</empty_at_start>
<driver_modified>no</driver_modified>
<data>
<data_text>Test52</data_text>
</data>
</im_field>
<im_field>
<field_number>6</field_number>
<empty_at_start>yes</empty_at_start>
<driver_modified>yes</driver_modified>
<data>
<data_text>Test62</data_text>
</data>
</im_field>
<im_field>
<field_number>7</field_number>
<empty_at_start>yes</empty_at_start>
<driver_modified>yes</driver_modified>
<data>
<data_text>Test72</data_text>
</data>
</im_field>
<im_field>
<field_number>8</field_number>
<empty_at_start>yes</empty_at_start>
<driver_modified>yes</driver_modified>
<data>
<data_date-time>09/08/09 09:08:00</data_date-time>
</data>
</im_field>
<im_field>
<field_number>9</field_number>
<empty_at_start>yes</empty_at_start>
<driver_modified>yes</driver_modified>
<data>
<data_date-time>09/08/09 08:09:00</data_date-time>
</data>
</im_field>
<im_field>
<field_number>10</field_number>
<empty_at_start>no</empty_at_start>
<driver_modified>no</driver_modified>
<data>
<data_text>Test102</data_text>
</data>
</im_field>
</formdata>
</imessage>
</pnet_message_history_packet_response>
一些C#-code
protected void GetMessages()
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.PreserveWhitespace = false;
Regex regex = new Regex(@"^\s+$[\r\n]*");
String cleanedXml = regex.Replace(postXMLData(URL, prefix, "POST"), "><").TrimStart();
xmldoc.LoadXml(cleanedXml);
XmlNodeList messageList = xmldoc.GetElementsByTagName("imessage");
foreach (XmlNode node in messageList)
{
XmlElement messageElement = (XmlElement)node;
String Arrival;
Arrival = messageElement.GetElementsByTagName("data_text")[0].InnerText;
testTxtBx.Text += Arrival; //I am just trying to write to a Textbox now to see the results.
}
}
請exaplin你需要什麼,你的問題是不明確你要分析這個XML在C#或你想在SQL中解析? –
嗨,你的編輯帶走了我的......在你的文字中,所有'都是隱形的。你必須把它們包裝起來。否則,這個問題是完全不清楚的...... –
Shnugo
編輯問題時,請務必小心,否則您將無法撤消之前的有益修改。 – Amy