下面我收到了我从网络服务收到的XML。我真的很擅长在C#中使用XML,但我只是在它们来自某个<data_text>
时尝试阅读<form_id>
元素。我想迭代所有的XML来写SQL发送的内容。有没有一种简单的方法来设置它,所以我可以从特定的<data_text>
找到<field_number>
说并将其分配给一个字符串,然后使用该字符串写入SQL?一旦我可以将所有内容都放入String中,我就可以轻松地写入SQL但我无法将XML转换为字符串。我也对其他选择持开放态度。在底部,我有目前的C#,我到目前为止。
<?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.
}
}
答案 0 :(得分:0)
或者,您已将c#发送到sql server存储过程...并允许存储过程将数据分解为@holder表............并执行从那个@holder表中进行CUD(创建,更新,删除)操作。
像这样:(没有显示存储过程创建,只显示相关的tsql代码)declare @xml xml
select @xml =
'
<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>
'
declare @holder table ( FormId int, DataText varchar(64) )
insert into @holder (FormId, DataText)
SELECT
T.MyEntity.value('../../form_id[1]', 'INT') AS ProgramId
,T.MyEntity.value('data_text[1]', 'VARCHAR(256)') AS SharedAccessKeyName
FROM @xml.nodes('pnet_message_history_packet_response/imessage/formdata/im_field/data') AS T(MyEntity);
select * from @holder