我需要解析一个 XML 文件并将数据存储到 vb.net 中的 sql server 中。我一直在网上阅读,看来我应该使用 xmltextReader 类来读取文件,因为这将消除内存不足的问题。我开始了类似以下的事情。我需要反馈我做得好还是应该使用其他更容易的方法。示例 XML 文件如下所示
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MainElement>
<MainEle1>
<InfoA>
<InfoAId>121480431A</InfoAId>
<firstName>Anne</firstName>
<lastName>Stevens</lastName>
<phone>6023122456</phone>
<address>
<address1>8735 Elm Road</address1>
<address2></address2>
<city>Nowhere</city>
<country></country>
<state>CA</state>
<suite></suite>
<zipCode>30141</zipCode>
</address>
<dob>09/08/1982</dob>
<gender></gender>
<primaryLanguage>Other</primaryLanguage>
<otherLanguage>french</otherLanguage>
<planName></planName>
<contacts>
<firstName>rajesh</firstName>
<lastName>raj</lastName>
<phone>1232343241</phone>
<address>
<address1></address1>
<address2></address2>
<city></city>
<country></country>
<state></state>
<suite></suite>
<zipCode></zipCode>
</address>
<contactType>EMERGENCY</contactType>
<relationship>friend</relationship>
<typeDesc>Emergency Contact/Next of Kin</typeDesc>
</contacts>
</InfoA>
</MainEle1>
<MainEle2>
<subMainEle2>
<firstName>victor</firstName>
<lastName>john</lastName>
<phone>1233455678</phone>
<address>
<address1>123 arrow</address1>
<address2></address2>
<city>upland</city>
<country></country>
<state>ca</state>
<suite>234</suite>
<zipCode>76547</zipCode>
</address>
<contactType>PRIMARY</contactType>
<relationship></relationship>
<typeDesc></typeDesc>
</subMainEle2>
</MainEle2>
<MainEle3>
..
</MailEle3>
<MainEle4>
..
</MailEle4>
..
</MainElement>
Dim reader As XmlTextReader
reader = New XmlTextReader("c:\FileName")
While Not reader.EOF
Select Case reader.NodeType
Case XmlNodeType.Element
Select Case reader.Name
Case "MainEle1"
Call ProcessMainEle1(reader) ' passing reader as ref
Case "MainEle2"
call ProcessMainEle2(reader) ' passing reader as ref
Case "MainEle3"
call ProcessMainEle3(reader) 'passing reader as ref
' and read many element
End Select
End Select
End While
在 Process 方法中,我使用类似的方法并创建插入语句。这是正确的方法还是对于元素中包含许多元素的大文件有一种简单的方法。我会感谢新手程序员的指导