我需要解析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 Whileand 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.
更多推荐
发布评论