C implements methods to improve xml read and write speed

  • 2020-11-30 08:30:26
  • OfStack

This article illustrates how C# can improve xml's read-write speed. Share to everybody for everybody reference. Specific implementation methods are as follows:

dim domxmldocument as system.xml.xmldocument  
  dim tmppath as string = apptempfilepath 
  dim xmlfile as string = tmppath + " \ testxml.xml" 
' Form loading event  
  private sub testxml_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load 
  ' read xml Process tests pass  
  dim domxmldocument as system.xml.xmldocument 
  dim tmppath as string = apptempfilepath 
  dim xmlfile as string = tmppath + " \ testxml.xml" 
  dim reader as system.xml.xmlreader = nothing 
  try 
  reader = new xml.xmltextreader(xmlfile) 
  'reader. 
  while reader.read 
  me.lboxxml.items.add(reader.name + reader.value) 
  end while 
  catch ex as exception 
  msgbox(ex.message) 
  finally 
  if not (reader is nothing) then 
  reader.close() 
  end if 
  end try 
  end sub 
  ' load xml The event  
  private sub btnxmlload_click(byval sender as system.object, byval e as system.eventargs) handles btnxmlload.click 
  'me.lboxxml.items.clear() 
  '' read xml Process tests pass  
  'dim reader as system.xml.xmlreader = nothing 
  'try 
  ' reader = new xml.xmltextreader(xmlfile) 
  ' while reader.read 
  ' me.lboxxml.items.add(reader.name + ":" + reader.value) 
  ' end while 
  'catch ex as exception 
  ' msgbox(ex.message) 
  'finally 
  ' if not (reader is nothing) then 
  ' reader.close() 
  ' end if 
  'end try 
  dim ds as new dataset 
  try 
  ' If you use it directly ds do datasource It doesn't unfold datagrid with dv Can directly display the correct.  
  ds.readxml(xmlfile) 
  dim tb as datatable 
  dim dv as dataview 
  tb = ds.tables(0) 
  dv = new dataview(tb) 
  datagrid1.datasource = dv 
  'datagrid1.datamember = "testxmlmember" 
  'datagrid1.datamember = "employeefname" 
  'dim dxd as new xmldatadocument 
  catch ex as exception 
  msgbox(ex.message.tostring) 
  end try 
  end sub 
  ' Save the new xml Content of the event  
  private sub btnsavenew_click(byval sender as system.object, byval e as system.eventargs) handles btnsavenew.click 
  dim mytw as new xmltextwriter(tmppath + " \ testxmlwrite.xml", nothing) 
  mytw.writestartdocument() 
  mytw.formatting = formatting.indented 
  mytw.writestartelement("team") 
  mytw.writestartelement("player") 
  mytw.writeattributestring("name", "george zip") 
  mytw.writeattributestring("position", "qb") 
  mytw.writeelementstring("nickname", "zippy") 
  mytw.writeelementstring("jerseynumber", xmlconvert.tostring(7)) 
  mytw.writeendelement() 
  mytw.writeendelement() 
  mytw.writeenddocument() 
  mytw.close() 
  end sub

In the case of large files, consider implementing the data update adapter manually, such as manually implementing 1 xml node search/update so that you don't have to rewrite the entire xml.
If the program's i/o is not a major problem, use the entity class to write the entire update, since the integrity of the data is bit 1.
If it is an article type, set up an xml index file to store the article number, url, etc., use attribute of xml to mark different fields, content pages can be stored with another html or xml page, and use linq to xml to operate data, the efficiency is not very poor, personal opinion. When searching, simply query the specified file name xml or file type.

Hopefully this article has helped you with your C# programming.


Related articles: