Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 3rd, 2007, 08:05 AM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Create XML file - add elements according to range

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>

Codes:

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.
 
Old October 3rd, 2007, 08:27 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Is this a XSLT problem? If not its in the wrong forum.

Why not just sort your sql, and then check the current value you to see if you need to open a new EmpRange element (and close the old one)?

/- Sam Judson : Wrox Technical Editor -/
 
Old October 3rd, 2007, 08:36 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well it's not in the XSLT forum and it doesn't mention XSLT so that seems okay to me :)
You seem to have three options, none of which are mutually exclusive:
  • Improve the SQL to get it nearer to what you need in the first place as Sam suggested
  • Use a DataSet/DataTable instead of a reader, you can sort the data after it comes from the database and again get it to a grouped format as in option one
  • Apply a transform after the initial creation, this is probably a last resort but would allow grouping, easily done with XSLT 2.0
If none of these are possible then state why and can you clarify that there will normally be more than one EmpItem in each EmpRange?

--

Joe (Microsoft MVP - XML)
 
Old October 3rd, 2007, 09:57 AM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

in my database, i have more than 50-60 tables. and i need to check each of thoses 50-60 tables and create XML. and sometimes same items were repeating in different tables. so i dont need to dupicate items in XML. so one i get all the values from database only i can sort or Group according to the range.

and in each EmpRange we have lots of empitems and it varies the count. and inside XML file its more than 1000 EmpItems.

so now i'm taking all the values from database to a MemoryStream. so wheteher i can sort the values that are in MemoryStream. and create the XML in new formate....
 
Old October 3rd, 2007, 11:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sounds like you should be writing some SQL to extract the data you actually want, rather than extracting everything into XML and then sorting it out later. SQL has some quite good facilities for selecting data and eliminating duplicates, if I remember right from 20 years ago...

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 3rd, 2007, 02:50 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Sorry, my mistake, I had my XSLT hat on and got the forums confused.

Try simply changing the SQL to read something like "Select ItemID,pl11,pl12 from MG10 where ItemID <> 0 ORDER BY ItemID"

Then your loop code might look like this:

Code:
Dim rangeOpen as Boolean = False
Dim rangeMax As Integer = -1
Dim cvValue As Integer

While myReader.Read
   cvValue = myReader.GetInt32(0)
   If cvValue > rangeMax Then
      ' Close old <EmpRange> if we're opened one
      If rangeOpen Then myXWriter.WriteEndElement()
      ' Find new range maximum
      While cvValue > rangeMax
         rangeMax = rangeMax + 100
      Wend
      'Open new <EmpRange>
      myXWriter.WriteStartElement("EmpRange")
      myXWriter.WriteAttributeString("min", rangeMax - 99)
      myXWriter.WriteAttributeString("max", rangeMax)
      rangeOpen = True
   End If

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

If rangeOpen Then myXWriter.WriteEndElement()
I haven't tested this code, but it looks right :)

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL file can't find elements in XML data? Mateo1041 XSLT 2 September 18th, 2008 09:37 AM
Create XML file - According to the Value, add elem remya1000 General .NET 0 October 2nd, 2007 11:03 PM
Problem to create an xml file from two xml files saurabh_inblore XSLT 1 April 12th, 2006 02:58 AM
create xml file using c# kk_katepally General .NET 2 February 11th, 2005 02:59 AM
How to replace elements(from existed XML file)? hbcontract XML 1 October 30th, 2003 05:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.