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.
1 2086
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: -
myXWriter.WriteStartElement("Item")
-
myXWriter.WriteAttributeString("CV", myReader(0))
-
myXWriter.WriteAttributeString("PL1", myReader(1))
-
myXWriter.WriteAttributeString("PL2", myReader(2))
-
myXWriter.WriteEndElement()
-
the XML should be: -
<Item CV="120" PL1="6" PL2="3" />
-
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: -
-
Dim doc As New XmlDocument
-
Dim nodeEmp As XmlNode 'Employee node
-
'Three nodes to hold the ranges
-
Dim nodeRange1 As XmlNode
-
Dim nodeRange2 As XmlNode
-
Dim nodeRange3 As XmlNode
-
Dim attrib As XmlNode
-
-
'Instantiate the nodes
-
nodeEmp = doc.CreateElement("Employee")
-
nodeRange1 = doc.CreateElement("EmpRange")
-
nodeRange2 = doc.CreateElement("EmpRange")
-
nodeRange3 = doc.CreateElement("EmpRange")
-
-
'Set attributes
-
attrib = doc.CreateAttribute("min")
-
-
attrib.value = "0"
-
nodeRange1.appendChild(attrib)
-
-
' Set the other min attributes...
-
-
attrib = doc.CreateAttribute("max")
-
attrib.value = "99"
-
nodeRange1.appendChild(attrib)
-
-
' Set the other max attributes...
-
-
Dim item As XmlNode
-
-
Dim strCV As String
-
-
While myReader.Read
-
-
strCV = myReader(0)
-
item = doc.CreateElement("EmpItem")
-
-
attrib = doc.CreateElement("CV")
-
attrib.value = strCV
-
item.appendChild(attrib)
-
-
attrib = doc.CreateElement("PL1")
-
attrib.value = myReader(1)
-
item.appendChild(attrib)
-
-
attrib = doc.CreateElement("PL2")
-
attrib.value = myReader(2)
-
item.appendChild(attrib)
-
-
'Determine which EmpRange node to add it to
-
If CInt(strCV)<100 Then
-
nodeRange1.appendChild(item)
-
ElseIf CInt(strCV)< 200 Then
-
nodeRange2.appendChild(item)
-
ElseIf CInt(strCV)<300 Then
-
nodeRange3.appendChild(item)
-
Else
-
'Do something...
-
End If
-
End While
-
-
' Append to Employee node
-
nodeEmp.appendChild(nodeRange1)
-
-
'append others...
-
-
doc.appendChild(nodeEmp)
-
doc.save("filename")
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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");
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| | |