解析大型XML文件并在vb中将数据插入SQL Server(parsing big XML file and inserting data to SQL server in vb)

编程入门 行业动态 更新时间:2024-10-24 00:24:07
解析大型XML文件并在vb中将数据插入SQL Server(parsing big XML file and inserting data to SQL server in vb)

我需要解析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方法中,我使用类似的方法并创建insert语句。 这是正确的方法还是有一个简单的方法用于大型文件,其中元素中包含许多元素。 我很感激新手程序员的指导

I need to parse an XML file and store the data to sql server in vb.net. I have been reading online and it seems that I should use xmltextReader class to read the file, since this will eliminate the out of memory issue. I started something like following. I need feedback that am I doing OK or should I used some other way that will be easier. Sample XML file goes like this

<?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

and in Process method I am using similar approach and creating the insert statement. Is that the right approach or is there a simple approach for large file with many element within elements. I would appreciate guidance for novice programmer

最满意答案

对于您的用例使用不基于DOM的XML Parser非常重要。 (SAXParser的)

从内存消耗的角度来看,IT中没有比基于DOM的XML解析器更糟糕的了。 这样的解析器需要比低级解析多100到1000倍的内存,这对你来说更有用。

For your use case It is important to use an XML Parser which is not DOM based. (SAXParser)

From memory consumption point of view there is nothing worse in IT than a DOM based XML parser. Such a parser will need 100 - 1000 times more memory than a low level parsing, which is more work for you.

更多推荐

本文发布于:2023-07-23 18:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   中将   文件   数据   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!