读取XML文件到表
问题描述:
大家好!
我是xml的新手.所以我需要帮助使程序读取xml文件.
例如:
Hi, everybody!
I''m newcomer in xml. so i need help make program read xml file.
example:
<interaction channelVariable="Seller2CreditCardC" name="creditCardCheck" operation="creditCardCheck" >
<participate toRole="CreditCardRole" relationshipType="SellerCreditCheck" fromRole="SellerRole"/>
<exchange action="request" name="creditCardCheck" informationType="creditCardCheckType">
<send variable="cdl:getVariable(creditCardValidity)"/>
<receive variable="cdl:getVariable(creditCardValidity)"/>
</exchange>
</interaction>
<interaction channelVariable="Buyer2SellerC" name="purchaseReply" operation="purchaseReply" >
<participate toRole="SellerRole" relationshipType="BuyerSeller" fromRole="BuyerRole"/>
<choice>
<workunit name="Send purchase confirmation" repeat="false" guard="(creditCardValidity) >0 and (storeAmount) >0 ">
<exchange action="respond" name="purchaseConfirm" informationType="creditCardCheckType">
</exchange>
</workunit>
<workunit name="Send purchase reject" repeat="false" guard="(creditCardValidity) = 0 or (storeAmount) =0 ">
<exchange action="respond" name="purchaseReject" informationType="creditCardCheckType">
</exchange>
</workunit>
</choice>
</interaction>
答
有许多方法可用于读取C#中的XML.您可以使用System.Xml或System.Linq.Xml来读取XML.看看这些使用第一种方法的帖子:
http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTasp /a> [ ^ ]
将XmlReader类与C#一起使用 [
Hi,
There are many ways which can be used to read XML in C#. You can use System.Xml or System.Linq.Xml to read XML. Take a look at these post which the first method is used:
http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx[^]
Using the XmlReader class with C#[^]
I hope it helps,
Cheers
这是代码.
Hi, Here is code.
Using reader As XmlReader = XmlReader.Create(filenametextbox)
While reader.Read()
' Check for start elements.
If reader.IsStartElement() Then
' Interaction (read attribute example name, initiate)
If reader.Name = "interaction" Then
'Console.WriteLine("Start <interaction> element.")
attr_name = reader("name")
attr_init = reader("initiate")
'If attr_name IsNot Nothing Then
'Console.WriteLine(" Has attribute name: {0}", attr_name)
'ElseIf attr_init IsNot Nothing Then
'Console.WriteLine(" Has attribute name: {0}", attr_init)
'End If
' participate (read attribute example toRole, fromRole)
ElseIf reader.Name = "participate" Then
' Get toRole,fromRole attribute.
attr_toRole = reader("toRole")
attr_fromRole = reader("fromRole")
from_to_role = ngoac1 & attr_fromRole & sym2 & attr_toRole & ngoac2
ElseIf reader.Name = "workunit" Then
wk_guard = reader("guard")
If InStr(1, wk_guard, " or ") > 0 Then
wk_guard_tem = wk_guard.Replace(" or ", hoac)
End If
If InStr(1, wk_guard, " and ") > 0 Then
wk_guard_tem = wk_guard.Replace(" and ", va)
End If
' exchange (read attribute example name)
ElseIf reader.Name = "exchange" Then
attr_name = reader("name")
' send (read attribute example variable)
ElseIf reader.Name = "send" Then
attr_variable_send = reader("variable")
attr_variable_send_tem = Mid(attr_variable_send, 17, Len(attr_variable_send) - 17)
' receive (read attribute example variable)
ElseIf reader.Name = "receive" Then
attr_variable_receive = reader("variable")
attr_variable_receive_tem = Mid(attr_variable_receive, 17, Len(attr_variable_receive) - 17)
End If
Else
If reader.Name = "exchange" And reader.NodeType = XmlNodeType.EndElement Then
End If
End If
End While
End Using
</interaction>