473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Create XML file - According to the Value, add elements under corresponding range

115 100+
i'm using VB.NET.

i'm using this codes to create this XML.

<Employee>
<EmpItem CV="120" PL1="2" PL2="3" /EmpItem>
<EmpItem Cv="2" PL1="6" PL2="4" /EmpItem>
<EmpIten Cv="234" PL1="4" PL"4" /EmpItem>
<DataName>
</Employee>



Code:

TextBox1.Text = "C:\Program\Emp.mdb"
If File.Exists(TextBox1.Text) Then
Dim strSQL As String = "Select ItemID,pl11,pl12 from MG10 where ItemID <> 0"
Dim myConnection As New OleDbConnection(strConn)
myConnection.Open()
Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection)
Dim myReader As OleDbDataReader = myCommand.ExecuteReader

Dim myXWriter As XmlTextWriter
Dim myWriter As StreamWriter
Dim myStream As MemoryStream
myStream = New MemoryStream

myXWriter = New XmlTextWriter(myStream, Encoding.UTF8)
myXWriter.Formatting = Formatting.Indented
myXWriter.Indentation = 2

myXWriter.WriteStartDocument()
myXWriter.WriteStartElement("Employee")

While myReader.Read
myXWriter.WriteStartElement("Item")
myXWriter.WriteAttributeString("CV", myReader(0))
myXWriter.WriteAttributeString("PL1", myReader(1))
myXWriter.WriteAttributeString("PL2", myReader(2))
myXWriter.WriteEndElement()
End While

myXWriter.WriteFullEndElement()
myXWriter.WriteFullEndElement()
myXWriter.WriteEndDocument()
myXWriter.Flush()

myStream.Seek(0, SeekOrigin.Begin)

Dim strConfig2 As String = New StreamReader(myStream).ReadToEnd()
myWriter = File.CreateText("C:\Example.xml")
myWriter.WriteLine(strConfig2)
myWriter.Close()
End If


anyway is there to create an XML like this...

<Employee>
<EmpRange min="0" max="99">
<EmpItem Cv="2" PL1="6" PL2="4">
</EmpRange>
<EmpRange min="100" max="199">
<EmpItem CV="120" PL1="2" PL2="3">
</EmpRange>
<EmpRange min="200" max="299">
<EmpIten Cv="234" PL1="4" PL"4">
<EmpRange>
<DataName>
</Employee>



according to the CV values can i add the EmpItems in corresponding Range. and the CV values will be random numbers. so if CV value is 34 then it should be under <EmpRange min="0" max="99">. likewise can we create an XML file to add EmpItems according to the range the CV value.

if you have any idea how to create this please help me... if you can provide any help then it will be a great help for me....

thanks in advanace.
Oct 3 '07 #1
1 2086
phvfl
173 Expert 100+
Hi,

Could you please use code tags when posting as it make it easier to read the post.

Firstly is the code provided actually producing the XML stated? From the code:
Expand|Select|Wrap|Line Numbers
  1. myXWriter.WriteStartElement("Item")
  2. myXWriter.WriteAttributeString("CV", myReader(0))
  3. myXWriter.WriteAttributeString("PL1", myReader(1))
  4. myXWriter.WriteAttributeString("PL2", myReader(2))
  5. myXWriter.WriteEndElement()
  6.  
the XML should be:
Expand|Select|Wrap|Line Numbers
  1. <Item CV="120" PL1="6" PL2="3" />
  2.  
The tag name is different and it closes correctly (no tag name after the "/" on a self closing tag.)

There are two methods that come to mind for how you could achieve what you ask. The first is to write the document as you do now and then use an XSLT to transform the file into the desired format. The second is to build the XML file using XmlNodes and appending them to the document. An example of this would be:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim doc As New XmlDocument
  3. Dim nodeEmp As XmlNode 'Employee node
  4. 'Three nodes to hold the ranges
  5. Dim nodeRange1 As XmlNode
  6. Dim nodeRange2 As XmlNode
  7. Dim nodeRange3 As XmlNode
  8. Dim attrib As XmlNode
  9.  
  10. 'Instantiate the nodes
  11. nodeEmp = doc.CreateElement("Employee")
  12. nodeRange1 = doc.CreateElement("EmpRange")
  13. nodeRange2 = doc.CreateElement("EmpRange")
  14. nodeRange3 = doc.CreateElement("EmpRange")
  15.  
  16. 'Set attributes
  17. attrib = doc.CreateAttribute("min")
  18.  
  19. attrib.value = "0"
  20. nodeRange1.appendChild(attrib)
  21.  
  22. ' Set the other min attributes...
  23.  
  24. attrib = doc.CreateAttribute("max")
  25. attrib.value = "99"
  26. nodeRange1.appendChild(attrib)
  27.  
  28. ' Set the other max attributes...
  29.  
  30. Dim item As XmlNode
  31.  
  32. Dim strCV As String
  33.  
  34. While myReader.Read
  35.  
  36. strCV = myReader(0)
  37. item = doc.CreateElement("EmpItem")
  38.  
  39. attrib = doc.CreateElement("CV")
  40. attrib.value = strCV
  41. item.appendChild(attrib)
  42.  
  43. attrib = doc.CreateElement("PL1")
  44. attrib.value = myReader(1)
  45. item.appendChild(attrib)
  46.  
  47. attrib = doc.CreateElement("PL2")
  48. attrib.value = myReader(2)
  49. item.appendChild(attrib)
  50.  
  51. 'Determine which EmpRange node to add it to
  52. If CInt(strCV)<100 Then
  53. nodeRange1.appendChild(item)
  54. ElseIf CInt(strCV)< 200 Then
  55. nodeRange2.appendChild(item)
  56. ElseIf CInt(strCV)<300 Then
  57. nodeRange3.appendChild(item)
  58. Else
  59. 'Do something...
  60. End If
  61. End While
  62.  
  63. ' Append to Employee node
  64. nodeEmp.appendChild(nodeRange1)
  65.  
  66. 'append others...
  67.  
  68. doc.appendChild(nodeEmp)
  69. doc.save("filename")
  70.  
This would create the XML document and save it to the specified file. This is slower than creating a file using an XmlTextWriter, but I think it would be quicker that using the writer and then transforming it using an XSLT. I have only used one attribute node and recreated it each time I wanted to use a new attribute, you could use multiple XmlNode variables instead.
Oct 4 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Peter A. Schott | last post by:
I've got a file that seems to come across more like a dictionary from what I can tell. Something like the following format: ###,1,val_1,2,val_2,3,val_3,5,val_5,10,val_10...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
9
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
8
by: Darius Fatakia | last post by:
Hello, I have a file that I have opened for reading and this file contains lines with several different types of constraint information. For example, here are a few lines: length(0) = 10...
4
by: L. | last post by:
Hello, I need to generate random Matrices (say of size 5*5), each with an average of X (say X=0.5), but with different values’ range. One matrix should have values in the range of 0-1, while...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
10
by: Charles Law | last post by:
In a VB.NET app, I can write Dim s As String = 3.ToString This is because 3 is of type Int32, which is a structure, which has a method ToString. I would also like to be able to write Dim...
8
by: Shalaka Joshi | last post by:
Hi, I have binary file say, "test.bin". I write "FF" in the file and expect my code to read 255 for me. char * lbuf; int lreadBytes ; long lData; FILE *pFile = fopen ("c:\\testbin","rb");
4
by: Abdhul Saleem | last post by:
Hi, I am recieving error ActiveX component can't create object in the following line in the asp page. set ExcelApp = CreateObject("Excel.Application") Previously this code was working fine....
0
by: remya1000 | last post by:
i'm using VB.NET. i'm using this codes to create this XML. TextBox1.Text = "C:\Program\Emp.mdb" If File.Exists(TextBox1.Text) Then Dim strSQL As String = "Select...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.