|
-
Aug 28th, 2012, 05:11 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] How to add xml schema to xmlwriter output
Have a datagridview that a user adds to based on other actions. When all down I need to take the data in the datagridview and save to a xml file in xml format with the schema included. In my print command I can create the schema code and put in textbox and create a xml file but how do I write the schema info inside the root node?
Here is my code now.
Code:
Imports System.Xml
Imports System.Xml.Schema
Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
If PrintToPrinterToolStripMenuItem.Checked = True Then
'....... printer
Else
Dim sLogFolder As String = Environment.CurrentDirectory & "\Logs\"
Dim sLogFileName As String = DateTime.Now.ToString("yyyyMMdd-hhmmss") & "_reportdata-file.xml"
Dim iMaxRows As Integer = dgvItems.Rows.Count - 1
Dim schema As New XmlSchema()
Dim elementReport As New XmlSchemaElement()
schema.Items.Add(elementReport)
elementReport.Name = "ReportData"
Dim ctReport As New XmlSchemaComplexType()
elementReport.SchemaType = ctReport
Dim sqReport As New XmlSchemaSequence()
ctReport.Particle = sqReport
Dim eCheckAmt As New XmlSchemaElement()
sqReport.Items.Add(eCheckAmt)
eCheckAmt.Name = "CheckAmount"
eCheckAmt.MinOccurs = 0
eCheckAmt.SchemaTypeName = New XmlQualifiedName("decimal", "http://www.w3.org/2001/XMLSchema")
Dim eCheckNum As New XmlSchemaElement()
sqReport.Items.Add(eCheckNum)
eCheckNum.Name = "CheckNumber"
eCheckNum.MinOccurs = 0
eCheckNum.SchemaTypeName = New XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema")
Dim eCustID As New XmlSchemaElement()
sqReport.Items.Add(eCustID)
eCustID.Name = "CustID"
eCustID.MinOccurs = 0
eCustID.SchemaTypeName = New XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema")
Dim eCustName As New XmlSchemaElement()
sqReport.Items.Add(eCustName)
eCustName.Name = "CustName"
eCustName.MinOccurs = 0
eCustName.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
Dim eInvoiceNum As New XmlSchemaElement()
sqReport.Items.Add(eInvoiceNum)
eInvoiceNum.Name = "InvoiceNum"
eInvoiceNum.MinOccurs = 0
eInvoiceNum.SchemaTypeName = New XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema")
Dim eInvoiceBal As New XmlSchemaElement()
sqReport.Items.Add(eInvoiceBal)
eInvoiceBal.Name = "InvoiceBal"
eInvoiceBal.MinOccurs = 0
eInvoiceBal.SchemaTypeName = New XmlQualifiedName("decimal", "http://www.w3.org/2001/XMLSchema")
Dim eTerms As New XmlSchemaElement()
sqReport.Items.Add(eTerms)
eTerms.Name = "Terms"
eTerms.MinOccurs = 0
eTerms.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
Dim eOrderNum As New XmlSchemaElement()
sqReport.Items.Add(eOrderNum)
eOrderNum.Name = "OrderNum"
eOrderNum.MinOccurs = 0
eOrderNum.SchemaTypeName = New XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema")
Dim nsmgr As New XmlNamespaceManager(New NameTable())
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema")
Dim sw As New IO.StringWriter
schema.Write(sw, nsmgr)
'right now just writing to a textbox but need to add to below after root node, before data
txtSQL.Text = sw.ToString
Dim xmlSettings As New XmlWriterSettings()
xmlSettings.Indent = True
xmlSettings.IndentChars = " "
xmlSettings.Encoding = System.Text.Encoding.UTF8
Using xmlWriter As XmlWriter = xmlWriter.Create(sLogFileName, xmlSettings)
xmlWriter.WriteProcessingInstruction("xml", "version='1.0'")
xmlWriter.WriteStartElement("ReportData")
'data
For i As Integer = 0 To iMaxRows
If dgvItems(0, 1).Value.ToString.Length > 0 Then
createNode(dgvItems(0, i).Value, dgvItems(1, i).Value, dgvItems(2, i).Value, dgvItems(3, i).Value, dgvItems(4, i).Value, dgvItems(5, i).Value, dgvItems(6, i).Value, _
dgvItems(7, i).Value, xmlWriter)
End If
Next
xmlWriter.WriteEndElement()
xmlWriter.WriteEndDocument()
xmlWriter.Close()
End Using
MessageBox.Show("File " & sLogFolder & sLogFileName & " created.")
End If
End Sub
Private Sub createNode(ByVal sCheckAmount As String, ByVal sCheckNumber As String, _
ByVal sCustID As String, ByVal sCustName As String, _
ByVal sInvNum As String, ByVal sInvBal As String, _
ByVal sTerms As String, ByVal sOrderNum As String, xw As XmlWriter)
xw.WriteStartElement("Entry")
xw.WriteStartElement("CheckAmount")
xw.WriteString(sCheckAmount)
xw.WriteEndElement()
xw.WriteStartElement("CheckNumber")
xw.WriteString(sCheckNumber)
xw.WriteEndElement()
xw.WriteStartElement("CustID")
xw.WriteString(sCustID)
xw.WriteEndElement()
xw.WriteStartElement("CustName")
xw.WriteString(sCustName)
xw.WriteEndElement()
xw.WriteStartElement("InvoiceNum")
xw.WriteString(sInvNum)
xw.WriteEndElement()
xw.WriteStartElement("InvoiceBal")
xw.WriteString(sInvBal)
xw.WriteEndElement()
xw.WriteStartElement("Terms")
xw.WriteString(sTerms)
xw.WriteEndElement()
xw.WriteStartElement("OrderNum")
xw.WriteString(sOrderNum)
xw.WriteEndElement()
xw.WriteEndElement()
End Sub
Last edited by lleemon; Aug 30th, 2012 at 11:23 AM.
Reason: Resolved
-
Aug 30th, 2012, 11:21 AM
#2
Thread Starter
Fanatic Member
Re: How to add xml schema to xmlwriter output
Figured it out.
Change
Code:
txtSQL.Text = sw.ToString
to
Code:
Dim sSchema As String = sw.ToString
Then in the Using statement add a .WriteRaw like so
Code:
Using xmlWriter As XmlWriter = xmlWriter.Create(sLogFileName, xmlSettings)
xmlWriter.WriteProcessingInstruction("xml", "version='1.0'")
xmlWriter.WriteStartElement("ReportData")
xmlWriter.WriteRaw(sSchema) '<--- NEW LINE
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|