Using vwd2005 in asp.net 2, I am trying to create an xml document in memory so that it can be edited in a gridview and in turn be output to excel. As a file, I was able to load into gridview and edit, but in production, cannot use a file because the application is load balanced. In tryin to work with a memory stream object, i was getting:
System.InvalidCastException was unhandled by user code
Message="Xml type 'xdt:untypedAtomic' does not support a conversion from Clr type 'XmlDeclaration' to Clr type 'String'."
Source="System.Xml"
So I tried to create the document type, and am now getting this error:
System.InvalidOperationException was unhandled by user code
Message="Cannot insert the node in the specified location."
Any help would be appreciated.
Protected Sub MakeOL_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim Adapter As New VPNMasterTableAdapters.SRSrvcsEquipDefaultsTableAdapter
Dim Table As VPNMaster.SRSrvcsEquipDefaultsDataTable
Dim Row As VPNMaster.SRSrvcsEquipDefaultsRow
'Get the Selected Order List
Table = Adapter.GetSRSrvcsEquipDefaultsByListName(ddlOrderList.SelectedValue.ToString())
If Table.Rows.Count > 0 Then
Dim xOL As New XmlDocument()
Dim xType As XmlDocumentType
Dim xdecl As XmlDeclaration
Dim root As XmlNode = xOL.DocumentElement
Dim element As XmlNode
'get last integer for use in CSV to XML
Dim iPN As Integer
'Build XML Doc
xType = xOL.CreateDocumentType("OrderList", Nothing, Nothing, Nothing)
xOL.AppendChild(xType)
xdecl = xOL.CreateXmlDeclaration("1.0", Nothing, Nothing)
xOL.AppendChild(xdecl)
root = xOL.CreateElement("OrderList")
xOL.AppendChild(root)
'Create XML from the Selected Order List
For Each Row In Table.Rows()
element = xOL.CreateElement("item")
'Stock
element.AppendChild(xOL.CreateNode("element", "Stock", ""))
element("Stock").InnerText = Row.Stock
'Activity
element.AppendChild(xOL.CreateNode("element", "Activity", ""))
element("Activity").InnerText = Row.Activity
'EquipOwned
element.AppendChild(xOL.CreateNode("element", "EquipOwned", ""))
element("EquipOwned").InnerText = Row.EquipOwned
'Manufacturer
element.AppendChild(xOL.CreateNode("element", "Manufacturer", ""))
element("Manufacturer").InnerText = Row.Manufacturer
'EHSN
element.AppendChild(xOL.CreateNode("element", "EHSN", ""))
element("EHSN").InnerText = Row.EHSN
'Qty
element.AppendChild(xOL.CreateNode("element", "Qty", ""))
element("Qty").InnerText = Row.Qty
'Part Number
element.AppendChild(xOL.CreateNode("element", "PartNumber", ""))
element("PartNumber").InnerText = Row.PartNumber
'Description
element.AppendChild(xOL.CreateNode("element", "Description", ""))
element("Description").InnerText = Row.Description
'ID
element.AppendChild(xOL.CreateNode("element", "ID", ""))
element("ID").InnerText = Row.ID
iPN = Row.ID
root.AppendChild(element)
Next
'Convert CSV File to XML
If System.IO.File.Exists(GetCSV.Value) Then
Dim CSVReader As System.IO.StreamReader = New System.IO.StreamReader(GetCSV.Value)
Dim ColName(), ColValue(), line, header As String
Dim PN, Status, i As Integer
Do
i = i + 1
line = CSVReader.ReadLine()
element = xOL.CreateElement("item")
If i = 1 Then
header = Replace(line, " ", "")
ColName = header.Split(",")
PN = Array.IndexOf(ColName, "PartNumber")
Status = Array.IndexOf(ColName, "Status")
Else
If Not line Is Nothing Then
ColValue = line.Split(",")
If ColValue(Status) = "Installed" Then
'make view that sets up partnumbers with manufacturers
'use PN to look up necessary elements and add to XML
Dim csvAdapter As New VPNMasterViewsTableAdapters.EquipmentDataTableAdapter
Dim csvTable As VPNMasterViews.EquipmentDataDataTable
Dim csvRow As VPNMasterViews.EquipmentDataRow
Dim strManufacturer, strDescription As String
'Some items in csv may not have been entered in the equipment table and we
'need to accommodate for values that can't be looked up
csvTable = csvAdapter.GetData(ColValue(PN))
If csvTable.Rows.Count > 0 Then
csvRow = csvTable.Rows(0)
strManufacturer = csvRow.Manufacturer
strDescription = csvRow.Description
Else
strManufacturer = "unknown"
strDescription = "unknown"
End If
element.AppendChild(xOL.CreateNode("element", "Stock", ""))
element("Stock").InnerText = ""
'Activity
element.AppendChild(xOL.CreateNode("element", "Activity", ""))
element("Activity").InnerText = "R"
'EquipOwned
element.AppendChild(xOL.CreateNode("element", "EquipOwned", ""))
element("EquipOwned").InnerText = "C"
'Manufacturer
element.AppendChild(xOL.CreateNode("element", "Manufacturer", ""))
element("Manufacturer").InnerText = strManufacturer
'EHSN
element.AppendChild(xOL.CreateNode("element", "EHSN", ""))
element("EHSN").InnerText = i.ToString() + ".0"
'Qty
element.AppendChild(xOL.CreateNode("element", "Qty", ""))
element("Qty").InnerText = "1"
'Part Number
element.AppendChild(xOL.CreateNode("element", "PartNumber", ""))
element("PartNumber").InnerText = ColValue(PN)
'Description
element.AppendChild(xOL.CreateNode("element", "Description", ""))
element("Description").InnerText = strDescription
'ID
element.AppendChild(xOL.CreateNode("element", "ID", ""))
element("ID").InnerText = IIf(iPN = 0, i, iPN + i)
root.AppendChild(element)
End If
End If
End If
Loop Until line Is Nothing
CSVReader.Close()
End If
Dim xStream As New MemoryStream
Dim xWriter As New XmlTextWriter(xStream, System.Text.Encoding.UTF8)
xWriter.WriteStartDocument(True)
xWriter.WriteValue(xOL)
fName = xStream
'Save and Apply XML Doc
Dim xDS As New DataSet
xDS.ReadXml(xStream)
xDS.ReadXml(xStream)
gvEditOL.Visible = True
MakeOLLink.Visible = False
ExportLink.Visible = True
End If
lblMakeOL.Visible = False
GetCSV.Visible = False
lblOLSelect.Visible = False
ddlOrderList.Visible = False
End Sub
Regards,
Eric